将 QQuickView 元素添加到现有 window
Add QQuickView element to existing window
每次在 C++ 中使用 QTQuick 单击按钮时,我都试图向我的 window 添加一个元素。
我有一个C++ class:
customclass.cpp
void CustomClass::clicked() {
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl("qrc:///box.qml"));
QObject *rectangleContainer = (QObject*)findItemByName(DownloadManager::rootObjects, "rectangle");
// view.setParent(rectangleContainer); Does not work ?
view.setProperty("visible", "true");
view.show();
}
和两个qml文件:
main.qml
import com.acidic.customclass 1.0
ApplicationWindow {
visible: true
width: 1280
height: 800
CustomClass {
id: demo
}
Rectangle {
id: rectangle
objectName: "rectangle"
width: 200
height: 200
color: "#ffffff"
}
Button {
id: button
text: qsTr("Button")
onClicked: {s
demo.clicked();
}
}
}
box.qml
Item {
Text {
id: text1
text: qsTr("Box!")
font.pixelSize: 12
}
}
代码已经缩短了,但它仍然足以显示我的当前状态。
CustomClass::clicked
确实在单击按钮时被调用,但我的目标是创建 box.qml
的实例并将其作为子元素插入到 rectangle
内的元素 main.qml
.
不需要c++后端,这可以在qml中直接使用javascript。
您可以在 Javascript 中使用 Qt.createComponent()
添加动态对象。
创建并添加 javascript 资源 (componentCreation.js
),此脚本首先从 box.qml
和 Qt.createComponent()
,然后使用 createObject()
将该新组件作为子元素附加到 "rectangle"
元素:
componentCreation.js
代码:
var component;
var box;
function createBoxObject() {
component = Qt.createComponent("box.qml");
box = component.createObject(rectangle, {"x": 100, "y": 100});
}
就是这样,在main.qml中导入javascript,同时在按钮onClicked
中调用脚本:
import com.acidic.customclass 1.0
import "componentCreation.js" as MyScript
ApplicationWindow {
visible: true
width: 1280
height: 800
CustomClass {
id: demo
}
Rectangle {
id: rectangle
objectName: "rectangle"
width: 200
height: 200
color: "#ffffff"
}
Button {
id: button
text: qsTr("Button")
onClicked: {
MyScript.createBoxObject();
}
}
}
注意:我在资源中添加了 box.qml 以便直接访问。
创建的对象将成为 main 中矩形对象的子对象。
每次在 C++ 中使用 QTQuick 单击按钮时,我都试图向我的 window 添加一个元素。
我有一个C++ class:
customclass.cpp
void CustomClass::clicked() {
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl("qrc:///box.qml"));
QObject *rectangleContainer = (QObject*)findItemByName(DownloadManager::rootObjects, "rectangle");
// view.setParent(rectangleContainer); Does not work ?
view.setProperty("visible", "true");
view.show();
}
和两个qml文件:
main.qml
import com.acidic.customclass 1.0
ApplicationWindow {
visible: true
width: 1280
height: 800
CustomClass {
id: demo
}
Rectangle {
id: rectangle
objectName: "rectangle"
width: 200
height: 200
color: "#ffffff"
}
Button {
id: button
text: qsTr("Button")
onClicked: {s
demo.clicked();
}
}
}
box.qml
Item {
Text {
id: text1
text: qsTr("Box!")
font.pixelSize: 12
}
}
代码已经缩短了,但它仍然足以显示我的当前状态。
CustomClass::clicked
确实在单击按钮时被调用,但我的目标是创建 box.qml
的实例并将其作为子元素插入到 rectangle
内的元素 main.qml
.
不需要c++后端,这可以在qml中直接使用javascript。
您可以在 Javascript 中使用 Qt.createComponent()
添加动态对象。
创建并添加 javascript 资源 (componentCreation.js
),此脚本首先从 box.qml
和 Qt.createComponent()
,然后使用 createObject()
将该新组件作为子元素附加到 "rectangle"
元素:
componentCreation.js
代码:
var component;
var box;
function createBoxObject() {
component = Qt.createComponent("box.qml");
box = component.createObject(rectangle, {"x": 100, "y": 100});
}
就是这样,在main.qml中导入javascript,同时在按钮onClicked
中调用脚本:
import com.acidic.customclass 1.0
import "componentCreation.js" as MyScript
ApplicationWindow {
visible: true
width: 1280
height: 800
CustomClass {
id: demo
}
Rectangle {
id: rectangle
objectName: "rectangle"
width: 200
height: 200
color: "#ffffff"
}
Button {
id: button
text: qsTr("Button")
onClicked: {
MyScript.createBoxObject();
}
}
}
注意:我在资源中添加了 box.qml 以便直接访问。 创建的对象将成为 main 中矩形对象的子对象。