QVariant Class: Accessing Properties
QVariant Class Accessing Properties
One frequently encountered problem C++ developers have as they try to make things more object oriented, arises from the fact that primitive types of C++ (e.g., int, float, char*, etc.) do not derive from a common base class. They're called primitive because they're smaller, simpler, and used to compose more complex things.
We would like to be able to retrieve the value of any property through the following function:
[returntype] QObject::property(QString propertyName);
Templates are one way to address this issuetemplate functions cause code to be generated for each used type. In Qt, we have another object-oriented way to solve the problem: through the use of QVariant.
QVariant is a union wrapper[5] for all the basic types, as well as all permitted Q_PROPERTY types. You can create a QVariant as a wrapper around another typed value. It remembers its type, and has member functions for getting and setting its value.
[5] A union is a struct that declares several data members that are all 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 only store a value for one of the declared members.
QVariant has a rich interface for data conversion and validity checking. In particular, there is a toString() function that returns a QString representation for its different types. This class greatly simplifies the property interface.
Example 15.5 shows some client code for the CustProps class defined in Example 15.3.
Example 15.5. src/properties/testcustomerprops.cpp
[ . . . . ] int main() { CustProps cust; cust.setName("Falafal Pita"); <-- 1 cust.setAddress("41 Temple Street; Boston, MA; 02114"); cust.setPhone("617-555-1212"); cust.setType("Government"); <-- 2 ASSERT_EQUALS(cust.getType(), CustProps::Government); <-- 3 QString originalid = "834"; <-- 4 cust.setId(originalid); QVariant v = cust.property("Id"); <-- 5 QString str = v.toString(); ASSERT_EQUALS(originalid, str); QDate date(2003, 7, 15); cust.setProperty("DateEstablished", QVariant(date)); <-- 6 QDate anotherDate = cust.getDateEstablished(); <-- 7 ASSERT_EQUALS(date, anotherDate); cust.setId(QString("anotherId")); qDebug() << objToString(&cust); cust.setType(CustProps::Educational); qDebug() << " Educational=" << cust.getType(); cust.setType("BogusType"); qDebug() << " Bogus= " << cust.getType(); return 0; } (1)setting some simple properties (2)setting enum property as a string (3)comparing to enum value (4)setting a string property (5)getting it back as a QVariant through the QObject base class method (6)setting date properties, wrapped in QVariants (7)The date comes back through the type-specific getter. |
In Example 15.6, we show a reflective objToString() method that works on any class with Qt properties defined. It works by iterating through each property() value, in a way that is comparable to the java.lang.reflect interface.
Example 15.6. src/properties/testcustomerprops.cpp
[ . . . . ] QString objToString(const QObject* obj) { QStringList result; const QMetaObject *meta = obj->metaObject(); <-- 1 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); result += QString(" %1 %2 = %3;") .arg(qmp.type()) .arg(qmp.name()) .arg(obj->property(qmp.name()).toString()); } result += "};"; return result.join(" "); } (1)We introspect into the object via the QMetaObject. |
The program outputs an object's state in a C++-style format:
src/properties> ./properties class CustProps : public QObject { 10 objectName = ; 10 Id = anotherId; 10 Name = Falafal Pita; 10 Address = 41 Temple Street; Boston, MA; 02114; 10 Phone = 617-555-1212; 14 DateEstablished = 2003-07-15; 2 Type = 3; }; Educational= 2 Bogus= -1