How to find only those properties of a widget which are shown in the Qt Designer?

﹥>﹥吖頭↗ 提交于 2020-01-15 10:28:50

问题


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()));
}

回答1:


isDesignable() should tell if the property will be shown in Qt Designer.

As stated in the Qt Documentation:

The DESIGNABLE attribute indicates whether the property should be visible in the property editor of GUI design tool (e.g., Qt Designer). Most properties are DESIGNABLE (default true). Instead of true or false, you can specify a boolean member function.

Also it seems that read-only properties are not shown in Designer.

Following your example:

    // Print all available properties of a Widget:
    qDebug()<<qPrintable("Widget: QPushButton");
    QPushButton *object = new QPushButton(this);
    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);
        bool isReadOnly = metaproperty.isReadable() && !metaproperty.isWritable();
        bool isWinModal = metaproperty.name() == QString("windowModality"); // removed windowModality manually
        if(!isReadOnly && metaproperty.isDesignable(object) && !isWinModal){
            qDebug() << metaproperty.name();
        }
    }

This prints:

Widget: QPushButton
objectName
enabled
geometry
sizePolicy
minimumSize
maximumSize
sizeIncrement
baseSize
palette
font
cursor
mouseTracking
tabletTracking
focusPolicy
contextMenuPolicy
acceptDrops
toolTip
toolTipDuration
statusTip
whatsThis
accessibleName
accessibleDescription
layoutDirection
autoFillBackground
styleSheet
locale
inputMethodHints
text
icon
iconSize
shortcut
checkable
autoRepeat
autoExclusive
autoRepeatDelay
autoRepeatInterval
autoDefault
default
flat

But there is a few caveats about this:

  • Property visibility on 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.

Extacted from QAbstractButton definition:

Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable)
Q_PROPERTY(bool checked READ isChecked WRITE setChecked DESIGNABLE isCheckable NOTIFY toggled USER true)

  • So to achieve what you want I am ruling out read-only and windowModality properties but this is kind of hacky. I am not sure if there is better way of doing this.


来源:https://stackoverflow.com/questions/55355246/how-to-find-only-those-properties-of-a-widget-which-are-shown-in-the-qt-designer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!