3

我的应用程序提供了在运行时编辑某些 QML 对象属性的可能性。是否可以像 Qt 设计器那样显示 QML 属性进行编辑?

例如,我有 QML 文件

import QtQuick 2.0

Rectangle {
id: circle
color: "red"
border.color: "black"
border.width: 1

/* allow to modificate by user */

opacity: 0.5
width: 16
height: 16
radius: width*0.5
}

创建后,我想允许用户在运行时更改其某些属性。是否可以使用 Qt 设计器类/插件/任何东西来显示其属性并允许对其进行编辑?

我不想重新发明轮子。:)

4

1 回答 1

2

您可以使用以下代码在 CPP 中获取 QML 项指针

QQuickItem *item = engine.rootObjects().first()->findChild("objectNameHere");

然后,您可以使用以下代码浏览其属性

for(int i=0;i<item->metaObject()->propertyCount();++i) {
    // Here you can get the name of the property like
    qDebug() << "Name" << item->metaObject()->property(i).name();
    // Here you can get the type name of the property like
    qDebug() << "Name" << item->metaObject()->property(i).typeName();
    // Here you can check if it's a double type for example, and get the value and, set the value to ZERO again for example
    if(item->metaObject()->property(i).type() == QVariant::DOUBLE) {
    // Get the value
    qDebug() << "Value" << item->property(item->metaObject()->property(i).name()).toDouble();
    // Set the value to ZERO
    item->setProperty(item->metaObject()->property(i).name(), 0.0);    
}

在几分钟内,您可以创建一个通用 UI 来使用这种方法修改任何对象的属性,我猜

于 2014-10-16T15:19:48.483 回答