I'm not sure if something like this is possible, but I am attempting to dynamically generate a GUI based on properties that have been registered into Qt's property system. My assumption is that since I have registered a property using Q_PROPERTY() in this fashion:
Q_PROPERTY(propertyType propertyName WRITE setPropertyName READ getPropertyName NOTIFY propertynameSignal)
I should be able to retrieve the signature of the write or read functions for connection using the connect() function. The goal of this is to have a dialog connected to this object for modifying properties, but without having to hand write all of it. Is this possible, am going about it the wrong way, or should I just hardcode the dialog?
EDIT 1: I'll share some of the code (stripped down) I have which will hopefully make it more obvious as to what I'm trying to do:
Class declaration for particular class:
class MyObject : public QObject
{
Q_OBJECT
Q_PROPERTY(bool someBool READ getBool WRITE setBool)
public:
bool someBool;
bool getBool();
void setBool(bool);
}
QDialog subclass that points to this Object:
void MyDialog::generateUI()
{
const QMetaObject* metaObject = _MyObjectPtr->metaObject();
for (int i = 0; i < metaObject->propertyCount(); ++i)
{
QMetaProperty property = metaObject->property(i);
if (!strcmp(property.typeName(), "bool")
{
QCheckBox* checkBox = new QCheckBox(this);
bool state = metaObject->property(property->name()).toBool();
checkBox->setCheckState((state) ? Qt::Checked : Qt::Unchecked);
// Add checkBox to QDialog layout widgets here
}
}
}
Forgive all the My* renamings, but I need to keep my actual class/member names private.
I can confirm that the object pointed to by MyObjectPtr is being read from properly, because the starting values reflect values that I expect when I change them around. The trouble is connecting this back to that object. I can change the values inside the checkbox GUI-side, but the values aren't being sent to the actual object pointed to by _MyObjectPtr.
model = new QPropertyModel(pObject); mapper = new QDataWidgetMapper(model); mapper->setModel(model); mapper->addMapping(aTextEdit, model.column("property name"));
– Aspire