如何仅查找Qt Designer中显示的窗口小部件的那些属性?

时间:2019-03-26 10:46:31

标签: qt qwidget qt-designer

如何找到 Qt Designer在属性编辑器中显示的窗口小部件(例如QPushButton)的那些属性 ? 我可以使用以下代码找到所有属性,包括那些在Qt Designer中未显示的属性:

// 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 个答案:

答案 0 :(得分:2)

isDesignable()应该说明该属性是否将在Qt Designer中显示。

如Qt Documentation中所述:

  

DESIGNABLE属性指示该属性是否应在GUI设计工具(例如Qt Designer)的属性编辑器中可见。大多数属性是DESIGNABLE(默认)。您可以指定布尔成员函数来代替true或false。

此外,似乎只读属性未在Designer中显示。

按照您的示例:

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

此打印:

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

但是有一些注意事项:

  • 可以通过其他属性来设置Designer上的属性可见性。例如,仅当布尔属性checked设置为true时,setCheckable属性才可设计。

从QAbstractButton定义继承:

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

  • 因此要实现您想要的目标,我排除了只读属性和windowModality属性,但这有点怪异。我不确定是否有更好的方法。