如何仅找到 Qt 设计器中显示的小部件的那些属性? [英] How to find only those properties of a widget which are shown in the Qt Designer?

查看:48
本文介绍了如何仅找到 Qt 设计器中显示的小部件的那些属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

isDesignable() 应该表明该属性是否会显示在 Qt 设计器中.

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

如 Qt 文档中所述:

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

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.

此外,Designer 中似乎没有显示只读属性.

Also it seems that read-only properties are not shown in 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

但是有一些关于此的警告:

But there is a few caveats about this:

  • Designer 上的属性可见性可以通过其他属性设置打开和关闭.例如,checked 属性仅在布尔属性 setCheckable 设置为 true 时才可设计.
  • 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.

从 QAbstractButton 定义中提取:

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)

  • 所以为了实现你想要的,我排除了只读和 windowModality 属性,但这有点 hacky.我不确定是否有更好的方法来做到这一点.
    • 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.
    • 这篇关于如何仅找到 Qt 设计器中显示的小部件的那些属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆