gpt4 book ai didi

javascript - QML动态添加按钮

转载 作者:行者123 更新时间:2023-11-28 04:28:01 26 4
gpt4 key购买 nike

我写了一个简单的代码,问题是如何将按钮添加到第一页而不是第二页。我有三个 calsses(main.qml、componentCreation.js、ButtonD.qml)

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import "componentCreation.js" as MyScript

ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
property var position: 0

Rectangle{
id: main
color: "#FFFFFF"
width: parent.width
height: parent.height
x:200
y:0
focus: true

StackView{
id: stack
initialItem: firstPage
Component{
id: firstPage
Rectangle{
id:firstRect
Label{
text: "firstPage"
x:100
}
Button{
text: "next"
onClicked:{
MyScript.createSpriteObjects(position)
position+=50
stack.push(secondPage)
}
}
}
}
Component{
id: secondPage
Rectangle{
id:secondRect
Label{
text: "secondPage"
x:100
}
Button{
text: "back"
onClicked: stack.push(firstPage)
}
}
}
}
}
}

qt 网站中描述的 JavaScript 代码(componentCreation.js)

    var component;
var sprite;
function createSpriteObjects(number) {
component = Qt.createComponent("ButtonD.qml");
if (component.status == Component.Ready)
finishCreation(number);
else
component.statusChanged.connect(finishCreation);
}
function finishCreation(number) {
if (component.status == Component.Ready) {
sprite = component.createObject(main, {"x": 50, "y": number});
number += 50
if (sprite == null) {
console.log("Error creating object");
}
} else if (component.status == Component.Error) {
console.log("Error loading component:", component.errorString());
}
}

我要添加的按钮

import QtQuick 2.0
import QtQuick.Controls 1.4
Button{
text: "created"
}

最佳答案

what should I write in

sprite = component.createObject(main, {"x": 100, "y": 500});

instead of main? i want to add a button from the javascript code to the rectangle with id secondRect

createObject 的第一个参数是您想要添加对象的项目的 id,所以答案不是第二个矩形吗?

关于javascript - QML动态添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44871822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com