如何仅查找 Qt Designer 中显示的小部件的那些属性?
How to find only those properties of a widget which are shown in the Qt Designer?
我如何才能找到只有小部件(例如 QPushButton)的那些属性Qt Designer 在 属性 编辑器中显示?
我可以使用以下代码找到所有属性,包括那些未在 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()));
}
isDesignable() 应该告诉 属性 是否会在 Qt Designer 中显示。
如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.
此外,只读属性似乎未在 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
属性 仅当 boolean 属性 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
属性,但这有点老套。我不确定是否有更好的方法。
我如何才能找到只有小部件(例如 QPushButton)的那些属性Qt Designer 在 属性 编辑器中显示? 我可以使用以下代码找到所有属性,包括那些未在 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()));
}
isDesignable() 应该告诉 属性 是否会在 Qt Designer 中显示。
如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.
此外,只读属性似乎未在 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
属性 仅当 boolean 属性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
属性,但这有点老套。我不确定是否有更好的方法。