【问题标题】:How to find only those properties of a widget which are shown in the Qt Designer?如何仅查找 Qt 设计器中显示的小部件的那些属性?
【发布时间】:2019-08-16 17:22:42
【问题描述】:

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

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

【问题讨论】:

    标签: qt qwidget qt-designer


    【解决方案1】:

    isDesignable() 应该告诉该属性是否将显示在 Qt 设计器中。

    如Qt中所述Documentation

    DESIGNABLE 属性指示属性是否应在 GUI 设计工具(例如 Qt Designer)的属性编辑器中可见。大多数属性都是可设计的(默认为 true)。您可以指定布尔成员函数,而不是 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 属性只有在布尔属性 setCheckable 设置为 true 时才可设计。

    从 QAbstractButton 定义中提取:

    Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable)
    Q_PROPERTY(bool checked READ isChecked WRITE setChecked DESIGNABLE isCheckable NOTIFY toggled USER true)
    
    
    • 所以为了实现你想要的,我排除了只读属性和windowModality 属性,但这是一种hacky。我不确定是否有更好的方法。

    【讨论】:

    • 它没有按预期工作。例如,在设计器中,未显示以下 QPushButton 属性,但它们是可设计的:frameGeometry、normalGeometry、frameSize、childrenRect 等
    • 您提到的那些只读属性似乎从未在 Designer 上显示,无论它们是否可设计。
    • 也许,你可以修改你的代码来检查isReadable() 而不是isWritable()并排除这些。
    • 以下代码消除了许多不相关的属性,但我仍然可以看到一些属性,如 windowModality、frameGeometry、normalGeometry、childrenRect、windowIcon、windowIconText、windowOpacity、windowModified 等,这些属性未在 Qt 设计器中显示。这些属性仅针对应用程序的根/父窗口小部件(如 QMainWindow 或 QWidget)显示。代码:
    • 我认为您的代码是完美的,因为我注意到 Qt Designer 也使用这种 hacky 方法,示例,请参见第 1 行。 638 和 1907 在以下链接中:code.woboq.org/qt5/qttools/src/designer/src/lib/shared/…code.woboq.org/qt5/qttools/src/designer/src/components/…
    猜你喜欢
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 2020-03-22
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    相关资源
    最近更新 更多