首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何仅查找Qt设计器中显示的小部件的属性?

如何仅查找Qt设计器中显示的小部件的属性?
EN

Stack Overflow用户
提问于 2019-03-26 10:46:30
回答 1查看 725关注 0票数 2

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

代码语言:javascript
复制
// 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()));
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-26 13:05:21

isDesignable()应该知道Qt中是否会显示该属性。

如Qt 文档中所述

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

而且,只读属性似乎没有显示在设计器中。

跟随你的榜样:

代码语言:javascript
复制
    // 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();
        }
    }

这些指纹:

代码语言:javascript
复制
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

但关于这一点,有几点要注意:

  • 其他属性可以设置设计器上的属性可见性。例如,checked属性只有在布尔属性setCheckable设置为true时才是可设计的。

从QAbstractButton定义中提取:

代码语言:javascript
复制
Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable)
Q_PROPERTY(bool checked READ isChecked WRITE setChecked DESIGNABLE isCheckable NOTIFY toggled USER true)
  • 因此,为了实现您想要的,我排除了只读和windowModality属性,但这有点麻烦。我不知道是否有更好的办法这样做。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55355246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档