在CML中设置QML中的对象类型属性 [英] Setting object type property in QML from C++
问题描述
(编辑以添加更多上下文)
(Editted to add more context)
我开始使用QML,我想在QML类型上设置一些参考属性, QML对象(理想情况下,没有父/子关系,因为我想连接多个QML对象)。
I've started using QML and I'd like to set some sort of reference property on a QML type, linking two QML objects (ideally, without a parent/child relationship as I'd like to connect multiple QML objects).
例如,我有以下文件。
qmldir:
A 1.0 A.qml
A .qml
import QtQuick 2.2
Rectangle {
width: 100
height: 100
color: 'red'
// Other stuff later
}
main.qml:
import QtQuick 2.2
import QtQuick.Window 2.1
import "qrc:/"
Rectangle {
objectName: "Main window"
visible: true
width: 360
height: 360
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
property A referencedObject;
Rectangle {
id: subView
objectName: "subView"
}
}
我想要做的是在运行时为C ++创建的对象设置'referencedObject'的值。但是,设置属性的函数不允许使用QObject指针或QQuickItem指针,因为QVariant对象不能以这种方式构造。
What I'd like to do with this is set the value of 'referencedObject' to an object created by C++ at runtime. However, the function for setting properties doesn't allow for QObject pointers or QQuickItem pointers, as QVariant objects can't be constructed that way.
像:
QQmlComponent aTemplate(&engine, QString("qrc:/A.qml"), &parentObject);
QQuickItem* aInstance = aTemplate.create();
aInstance->setParent(&parentObject);
mainView.setProperty("referencedObject",aInstance); // Won't work.
我想在QML中保留'A'类型对象,因为a)比C ++对于这个,和b)它意味着一个单独的图形对象与自己的生活,和QML工作更好的用例。
I'd like to keep the 'A' type object in QML because a) less boilerplate than C++ for this, and b) its meant to be a separate graphical object with life of its own, and QML works better for that use-case.
感谢任何帮助。
推荐答案
以下示例说明如何设置从C ++中定义的QML中定制类型的对象的属性。
The following example shows how to set properties of objects of your custom type defined in QML from C++.
A.qml
import QtQuick 2.2
Rectangle {
width: 0
height: 0
color:"red"
}
main.qml
import QtQuick 2.2
Rectangle {
visible: true
width: 640
height: 480
property alias referencedObject:aProperty;
A
{
id:aProperty
objectName: "aPropertyObject"//Needed to access it from C++
}
}
现在我们定义了一个qml类型 A
。 main.qml 具有此自定义类型的属性。我们需要从C ++更改此对象的属性。让我们改变宽度,高度和颜色。
Now we have defined a qml type A
. main.qml has a property of this Custom type. We need to change properties of this object from C++. Lets change the width,height and color.
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QQuickItem>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlComponent aTemplate(&engine, QUrl(("qrc:///A.qml")));
QQuickItem* aInstance =qobject_cast<QQuickItem*>(aTemplate.create());
aInstance->setWidth(100);
aInstance->setHeight(100);
aInstance->setProperty("color",QColor("green"));
if(aInstance)
{
QQuickView *mainView = new QQuickView;
mainView->setSource(QUrl(QStringLiteral("qrc:///main.qml")));
mainView->show();
QQuickItem * aPropertyObject = mainView->rootObject()->findChild<QQuickItem*>("aPropertyObject");
if(aPropertyObject)
{
//Now you have pointers to both source and destination.
//You can write a helper function which assigns the values
//of source to the destination.
//For the sake of demonstration, I am just setting some properties.
aPropertyObject->setWidth(aInstance->width());
aPropertyObject->setHeight(aInstance->height());
aPropertyObject->setProperty("color",aInstance->property("color"));
}
}
return app.exec();
}
这篇关于在CML中设置QML中的对象类型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!