#include "main.h" #include #include #include #include // set -> get via property() static void test1 (int type) { for (Example::Color c : {Example::Red, Example::Green, Example::Blue}) { Example x; QVariant v = c; v.convert(type); x.setProperty("color", v); QVariant newv = x.property("color"); assert(newv.toInt() == x.property("color").toInt()); qDebug() << ((newv == c)?"pass":"FAIL") << type << c << v << newv; } } // set -> get via meta property static void test2 (int type) { QMetaProperty prop; for (int k = 0; k < Example::staticMetaObject.propertyCount(); ++ k) { prop = Example::staticMetaObject.property(k); if (!strcmp("color", prop.name())) break; } for (Example::Color c : {Example::Red, Example::Green, Example::Blue}) { Example x; QVariant v = c; v.convert(type); prop.write(&x, v); QVariant newv = prop.read(&x); assert(x.property("color") == prop.read(&x)); assert(newv.toInt() == prop.read(&x).toInt()); qDebug() << ((newv==c)?"pass":"FAIL") << type << c << v << newv; } } static void test3 (int type) { for (Example::Color c : {Example::Red, Example::Green, Example::Blue}) { QVariant v = c; v.convert(type); Example::Color newc = qvariant_cast(v); qDebug() << ((newc==c)?"pass":"FAIL") << type << c << v << newc; } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); for (auto test : {test1, test2, test3}) { qDebug() << "---"; test(QVariant::Int); test(QVariant::UInt); test(QVariant::LongLong); test(QVariant::ULongLong); } }