【问题标题】:Edit QML property from application like designer does像设计师一样从应用程序编辑 QML 属性
【发布时间】:2013-08-02 14:46:07
【问题描述】:

我的应用程序提供了在运行时编辑某些 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 设计器类/插件/任何东西来显示其属性并允许对其进行编辑?

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

【问题讨论】:

标签: qt qml qt5 qt-quick


【解决方案1】:

你可以通过以下代码在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 来让用这种方法修改任何对象的属性,我猜

【讨论】:

  • 是的,在这种情况下,我需要为不同类型实现基本的 UI 控件。但是,Qt Creater 已经有了它,所以我最初的问题是如何重用它。实际上,我已经找到了解决方案,它的 QQmlComponent + setData + qml 模板(TextInput { text: backendItem.%1.properties.%2} )但无论如何感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-16
  • 2017-02-27
  • 2018-01-13
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多