Created
February 23, 2016 18:57
-
-
Save mcleary/a613edd751033d0c7e59 to your computer and use it in GitHub Desktop.
Show how to iterate through properties declared with Q_PROPERTY macro of a QObject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QDebug> | |
#include <QMetaProperty> | |
#include <QPushButton> | |
int main(int argc, char *argv[]) | |
{ | |
QApplication a(argc, argv); | |
QPushButton m; | |
for(auto p : m.dynamicPropertyNames()) | |
{ | |
qDebug() << p; | |
} | |
auto object = &m; | |
const QMetaObject *metaobject = object->metaObject(); | |
int count = metaobject->propertyCount(); | |
for (int i=0; i<count; ++i) { | |
QMetaProperty metaproperty = metaobject->property(i); | |
const char *name = metaproperty.name(); | |
QVariant value = object->property(name); | |
qDebug() << name << | |
value << | |
"NOTIFY" << metaproperty.hasNotifySignal() << | |
"WRITABLE" << metaproperty.isWritable(); | |
if(!metaproperty.isWritable()) | |
{ | |
qDebug() << name << "READONLY" << value; | |
} | |
} | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment