Skip to content

Instantly share code, notes, and snippets.

@JC3
Created November 3, 2022 17:37
Show Gist options
  • Save JC3/81609c9527989de88355d0cec058f9e2 to your computer and use it in GitHub Desktop.
Save JC3/81609c9527989de88355d0cec058f9e2 to your computer and use it in GitHub Desktop.
Qt meta property ULongLong -> enum conversion tests
#include "main.h"
#include <QCoreApplication>
#include <QVariant>
#include <QMetaProperty>
#include <QDebug>
// 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<Example::Color>(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);
}
}
#ifndef MAIN_H
#define MAIN_H
#include <QObject>
class Example : public QObject {
Q_OBJECT
public:
enum Color { Red, Green, Blue, None };
Example () : color(None) { }
Q_ENUM(Color)
Q_PROPERTY(Color color MEMBER color)
private:
Color color;
};
#endif // MAIN_H
---
pass 2 Example::Red QVariant(int, 0) QVariant(int, 0)
pass 2 Example::Green QVariant(int, 1) QVariant(int, 1)
pass 2 Example::Blue QVariant(int, 2) QVariant(int, 2)
pass 3 Example::Red QVariant(uint, 0) QVariant(int, 0)
pass 3 Example::Green QVariant(uint, 1) QVariant(int, 1)
pass 3 Example::Blue QVariant(uint, 2) QVariant(int, 2)
FAIL 4 Example::Red QVariant(qlonglong, 0) QVariant(int, 3)
FAIL 4 Example::Green QVariant(qlonglong, 1) QVariant(int, 3)
FAIL 4 Example::Blue QVariant(qlonglong, 2) QVariant(int, 3)
FAIL 5 Example::Red QVariant(qulonglong, 0) QVariant(int, 3)
FAIL 5 Example::Green QVariant(qulonglong, 1) QVariant(int, 3)
FAIL 5 Example::Blue QVariant(qulonglong, 2) QVariant(int, 3)
---
pass 2 Example::Red QVariant(int, 0) QVariant(int, 0)
pass 2 Example::Green QVariant(int, 1) QVariant(int, 1)
pass 2 Example::Blue QVariant(int, 2) QVariant(int, 2)
pass 3 Example::Red QVariant(uint, 0) QVariant(int, 0)
pass 3 Example::Green QVariant(uint, 1) QVariant(int, 1)
pass 3 Example::Blue QVariant(uint, 2) QVariant(int, 2)
FAIL 4 Example::Red QVariant(qlonglong, 0) QVariant(int, 3)
FAIL 4 Example::Green QVariant(qlonglong, 1) QVariant(int, 3)
FAIL 4 Example::Blue QVariant(qlonglong, 2) QVariant(int, 3)
FAIL 5 Example::Red QVariant(qulonglong, 0) QVariant(int, 3)
FAIL 5 Example::Green QVariant(qulonglong, 1) QVariant(int, 3)
FAIL 5 Example::Blue QVariant(qulonglong, 2) QVariant(int, 3)
---
pass 2 Example::Red QVariant(int, 0) Example::Red
pass 2 Example::Green QVariant(int, 1) Example::Green
pass 2 Example::Blue QVariant(int, 2) Example::Blue
pass 3 Example::Red QVariant(uint, 0) Example::Red
pass 3 Example::Green QVariant(uint, 1) Example::Green
pass 3 Example::Blue QVariant(uint, 2) Example::Blue
pass 4 Example::Red QVariant(qlonglong, 0) Example::Red
pass 4 Example::Green QVariant(qlonglong, 1) Example::Green
pass 4 Example::Blue QVariant(qlonglong, 2) Example::Blue
pass 5 Example::Red QVariant(qulonglong, 0) Example::Red
pass 5 Example::Green QVariant(qulonglong, 1) Example::Green
pass 5 Example::Blue QVariant(qulonglong, 2) Example::Blue
@JC3
Copy link
Author

JC3 commented Nov 3, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment