2013-08-02 21 views
2

我的應用程序提供了在運行時編輯某些QML對象屬性的可能性。 是否可以像Qt設計器一樣顯示QML屬性進行編輯?像應用程序一樣編輯應用程序的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設計器類/插件/任何東西來顯示其屬性並允許編輯它們?

我不想重新發明輪子。 :)

+0

我發現,如何在Qt創建器中完成:http://qt.gitorious.org/qt-creator/qt-creator/blobs/4eac04fba059a513bb58703660adf516cd6ce6ac/src/plugins/qmldesigner/components/propertyeditor/propertyeditor.cpp需要了解的是可能重用 – Dmitry

回答

1

你可以在CPP的QML項指針下面的代碼

QQuickItem *項目= engine.rootObjects()第() - > 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);  
} 

通過其性質走在幾分鐘之內,你可以創建一個通用的用戶界面,讓這種方法修改任何對象的屬性,我想

+0

是的,在這種情況下,我需要爲不同類型實現基本的UI控件。但是,Qt Creater已經擁有它了,所以我最初的問題是如何重用它。實際上,我已經找到了解決方案,它的QQmlComponent + setData + qml模板(TextInput {text:backendItem。%1.properties。%2}),但無論如何感謝回覆。 – Dmitry