Solved Find those properties of a widget which are shown in the Qt Designer
-
How can I find only those properties of a widget (e.g. QPushButton) which Qt Designer shows in the Property Editor?
I can find all properties including those which are not shown in the Qt Designer using the following code:// Print all available properties of a Widget: qDebug()<<qPrintable("Widget: QPushButton"); QObject *object = new QPushButton; const QMetaObject *metaobject = object->metaObject(); for (int i=0; i<metaobject->propertyCount(); ++i) { QMetaProperty metaproperty = metaobject->property(i); const char *name = metaproperty.name(); QVariant value = object->property(name); qDebug()<<qPrintable("\n" + QString(name) + "\n" + QString(value.typeName())); }
-
Hi
I think you can check with
https://doc.qt.io/Qt-5/qmetaproperty.html#isDesignable
as docs says
"The DESIGNABLE attribute indicates whether the property should be visible in the property editor of GUI design tool (e.g., Qt Designer). " -
@mrjj It doesn't work as expected. For example, In the designer the following QPushButton properties are not shown but they are isDesignable: frameGeometry, normalGeometry, frameSize, childrenRect, etc
-
@Eager
Ok, try to dump all attribute and see if you can find a red line.
Maybe they also need to be https://doc.qt.io/Qt-5/qmetaproperty.html#isStored -
@mrjj isWritable() removed a lot of unrelated properties but still a few appear, for example:
windowModality, windowFilePath, windowModified, windowTitle etc.
I used isStored too but I still see the above properties. -
@Eager
Hmm. Im not sure how the Designer does it.
I tried to look around in
https://code.woboq.org/qt5/qt-creator/src/plugins/designer/
but found nothing. -
@mrjj Right now I am trying to look around in
https://code.woboq.org/qt5/qttools/src/designer/src/
Can you please look around in this one too, if you don't mind? -
@Eager
sure. if we can find the actual property widget we should get a clue. -
Hi,
IIRC, that's because you are parsing the current class meta object for properties.
You have go back the chain up to the "root" meta object.
-
@SGaist For finding only those properties of QPushButton which the Qt Designer displays in Property Editor, I am currently doing like this:
qDebug()<<qPrintable("Widget: QPushButton"); QObject *object = new QPushButton; const QMetaObject *metaobject = object->metaObject(); for (int i=0; i<metaobject->propertyCount(); ++i) { QMetaProperty metaproperty = metaobject->property(i); qDebug()<<metaproperty.isStored(); if(metaproperty.isWritable() && metaproperty.isDesignable()) { qDebug()<<qPrintable(QString(metaproperty.name()) + " : " + QString(metaproperty.typeName())); } }
Can you please point out where I am doing wrong?
-
You have go back the chain up to the "root" meta object.
How can I do this? I wish I knew this concept! :(
Can you please explain this or share a sample code?
I am already highly obliged! -
To go through the chain, use QMetaObject::superClass.
-
@SGaist I tried that and I noticed that QMetaObject::superClass() gives me the properties of the base classes of the widget in question. I went through the chain in a loop and displayed the properties of each level and I found that properties like windowModality, windowFilePath, windowModified, windowTitle etc. are defined inside the base classes alongwith many other which should be displayed for the QPushButton in the Qt Designer.
I think Qt Designer uses some kind of fuzzy logic and somehow suppresses some of the properties for the child widgets. -
What are the differences ?
-
At last I found a working solution. Of course, it has some limitations but works for my use case.
// Print all those properties of a widget which are shown in the Qt Designer: /* Main Container is the widget having no parent. * Main Container has the following extra properties: * - windowTitle * - windowIcon * - windowIconText * - windowOpacity * - windowModified * - windowFilePath * * In a ui form there can only be one Main Container. * * As we want to get the properties of the widgets which are not designed/intended * to be used as Main Container of a ui form so to avoid getting the above extra * properties we make them children of a fake main container widget. * * Qt Designer does not show read only properties so make sure you display only those * properties which are both readable and writable. * * Note also that property visibility in the Qt Designer can be set ON and OFF by * other properties. For example checked property is designable only if boolean * property setCheckable is set to true. * * As you can notice in the files: * qt5/qttools/src/designer/src/lib/shared/qdesigner_propertysheet.cpp * qt5/qttools/src/designer/src/components/formeditor/qdesigner_resource.cpp * that windowModality is set Visible only for the Main Container so manually remove that * property whenever you find it for the children widgets. */ QWidget *fakeMainContainerWidget = new QWidget(); // No parent means main container. QObject *object = new QPushButton(fakeMainContainerWidget); const QMetaObject *metaobject = object->metaObject(); qDebug() << qPrintable("-==" + QString(metaobject->className()) + " ==-"); for (int i=0; i<metaobject->propertyCount(); ++i) { QMetaProperty metaproperty = metaobject->property(i); bool isReadOnly = metaproperty.isReadable() && !metaproperty.isWritable(); bool isWinModal = metaproperty.name() == QString("windowModality"); // removed windowModality manually if( !isReadOnly && metaproperty.isDesignable(object) && !isWinModal ) { qDebug() << qPrintable(QString(metaproperty.name()) + " : " + QString(metaproperty.typeName())); } }
-
@SGaist I came up with a solution and I have posted it in this thread.