Skip to content

Instantly share code, notes, and snippets.

@mcleary
Created February 23, 2016 18:57
Show Gist options
  • Save mcleary/a613edd751033d0c7e59 to your computer and use it in GitHub Desktop.
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
#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