如何才能找到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()));
}发布于 2019-03-26 13:05:21
isDesignable()应该知道Qt中是否会显示该属性。
如Qt 文档中所述
DESIGNABLE属性指示该属性是否应在GUI设计工具的属性编辑器(例如,DESIGNABLE)中可见。大多数属性都是可设计的(默认为true)。可以指定布尔成员函数,而不是true或false。
而且,只读属性似乎没有显示在设计器中。
跟随你的榜样:
// 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但关于这一点,有几点要注意:
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属性,但这有点麻烦。我不知道是否有更好的办法这样做。https://stackoverflow.com/questions/55355246
复制相似问题