Qt 像设计器一样从应用程序编辑QML属性

Qt 像设计器一样从应用程序编辑QML属性,qt,qml,qt5,qt-quick,Qt,Qml,Qt5,Qt Quick,我的应用程序提供了在运行时编辑某些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 }

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


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

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

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);    
}
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);    
}
for(int i=0;imetaObject()->propertyCount();++i){
//在这里,您可以获得属性的名称,如
qDebug()属性(i).name();
//在这里,您可以获得属性的类型名称,如
qDebug()属性(i).typeName();
//例如,在这里,您可以检查它是否是双精度类型,并获取值,例如,再次将值设置为零
if(item->metaObject()->property(i).type()==QVariant::DOUBLE){
//获取值
qDebug()元对象()->property(i.name()).toDouble();
//将该值设置为零
item->setProperty(item->metaObject()->property(i).name(),0.0);
}

在几分钟内,你就可以创建一个通用的UI,让你用这种方法修改任何对象的属性,我想我已经找到了,在Qt creator中是如何做到的:需要理解重用Eyes是可能的,在这种情况下,我需要为不同的类型实现基本的UI控件。但是,Qt Creater已经有了它,所以我最初的问题是如何实现为了重用它.Actual,我已经找到了解决方案,它的QQmlComponent+setData+qml模板(TextInput{text:backendItem.%1.properties.%2}),但无论如何,感谢您的回复。