[ fromfile: reflection.xml id: qvariantaccess ]
To retrieve the value of any property, you call this method:
QVariant QObject::property(QString propertyName);
QVariant is a union
[91] wrapper for all the basic types and all permitted Q_PROPERTY
types.
You can create a QVariant as a wrapper around another typed value.
A QVariant remembers its type and has member functions for getting and setting its value.
QVariant has a rich interface for data conversion and validity checking.
In particular, there is a toString()
function that returns a QString representation for many of its supported types.[92]
This class greatly simplifies the property interface.
Example 12.5 shows how you can get and set the same property values via the direct getters and setters, or via the indirect property()
and setProperty()
methods.
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. |
<include src="src/properties/testcustomerprops.cpp" allfiles="true" segid="main" href="src/properties/testcustomerprops.cpp" mode="cpp" id="testcustomerpropscpp"/>
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"); }
<include src="src/properties/testcustomerprops.cpp" mode="cpp" href="src/properties/testcustomerprops.cpp" id="objToString" segid="objToString"/>
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 *********
<include src="src/properties/output.txt" href="src/properties/output.txt" mode="text"/>
[91] 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.
[92] See QVariant::canConvert()
for more details.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |