[ fromfile: reflection.xml id: qvariantaccess ]
Example 12.5. src/properties/testcustomerprops.cpp
[ . . . . ] #include "customer-props.h" void TestCustomerProps::test() { Customer cust; cust.setObjectName("Customer"); cust.setName("Falafal Pita"); cust.setAddress("41 Temple Street; Boston, MA; 02114"); cust.setPhone("617-555-1212"); cust.setType("Government"); QCOMPARE(cust.getType(), Customer::Government); QString originalid = "834"; cust.setId(originalid); QVariant v = cust.property("id"); QString str = v.toString(); QCOMPARE(originalid, str); QDate date(2003, 7, 15); cust.setProperty("dateEstablished", QVariant(date)); QDate anotherDate = cust.getDateEstablished(); QEXPECT_FAIL("", "These are different dates", Continue); QCOMPARE(date, anotherDate); cust.setId(QString("anotherId")); qDebug() << objToString(&cust); cust.setType(Customer::Educational); qDebug() << " Educational=" << cust.getType(); cust.setType("BogusType"); qDebug() << " Bogus= " << cust.getType(); return; } QTEST_MAIN(TestCustomerProps)
QObject function call. | |
Setting some simple properties. | |
Setting enum property as a string. | |
Comparing to enum value. | |
Setting a string property. | |
Getting it back as a QVariant through the QObject base class method. | |
Setting date properties, wrapped in QVariants. | |
The date comes back through the type-specific getter. |
Example 12.6 shows a reflective objToString()
method that works on any class with Qt properties defined.
It works by iterating through the indexed property()
values in a way that is comparable to the java.lang.reflect
interface.
Only the variant types that canConvert(QVariant::String)
will be printed.
Example 12.6. src/properties/testcustomerprops.cpp
[ . . . . ] QString objToString(const QObject* obj) { QStringList result; const QMetaObject* meta = obj->metaObject(); result += QString("class %1 : public %2 {") .arg(meta->className()) .arg(meta->superClass()->className()); for (int i=0; i < meta->propertyCount(); ++i) { const QMetaProperty qmp = meta->property(i); QVariant value = obj->property(qmp.name()); if (value.canConvert(QVariant::String)) result += QString(" %1 %2 = %3;") .arg(qmp.typeName()) .arg(qmp.name()) .arg(value.toString()); } result += "};"; return result.join("\n"); }
Introspect into the object via the QMetaObject. | |
Each property has a QMetaProperty. |
To build this program it is necessary for the project file to contain the line
CONFIG += qtestlib
The program outputs an object's state in a C++-style format:
Example 12.7. src/properties/output.txt
********* Start testing of TestCustomerProps ********* Config: Using QTest library 4.6.2, Qt 4.6.2 PASS : TestCustomerProps::initTestCase() QDEBUG : TestCustomerProps::test() "class CustProps : public QObject { QString objectName = Customer; QString id = anotherId; QString name = Falafal Pita; QString address = 41 Temple Street; Boston, MA; 02114; QString phone = 617-555-1212; QDate dateEstablished = 2003-07-15; CustomerType type = 3; };" QDEBUG : TestCustomerProps::test() Educational= 2 QDEBUG : TestCustomerProps::test() Bogus= -1 PASS : TestCustomerProps::test() PASS : TestCustomerProps::cleanupTestCase() Totals: 3 passed, 0 failed, 0 skipped ********* Finished testing of TestCustomerProps *********
[60] In C and C++, a union
is a struct
that can declare two or more alternative data members to be allocated at the same address. This means that the union
will occupy only enough memory to accommodate the largest of the declared data members. When instantiated, a union
can store only a value for one of the declared members.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |