gpt4 book ai didi

qt - 无法使用按钮 : QML 关闭窗口

转载 作者:行者123 更新时间:2023-12-02 08:28:12 26 4
gpt4 key购买 nike

我创建了一个项目,其中包含一个按钮。我正在尝试使用此按钮关闭项目的父窗口,但在单击项目时收到此消息:

TypeError: Property 'close' of object QQuickRootItem(0x1d8efed8) is not a function



你能帮我解决这个问题吗?

项目代码:
import QtQuick 2.4

Item {

id: backButton

ItemForButton{

id: baseButton
text: "Back"

onClicked: {

backButton.parent.close()
}

}

}

窗口代码:
Window {

id: window
visible: true
BackButton {

}
x: 30
y: 30
}

最佳答案

好像有点乱。如果我是你,我会为自定义按钮类型添加一个点击信号。例如:

元素:

import QtQuick 2.4

Item {
id: backButton

// Add a clicked signal here
signal clicked()

ItemForButton{

id: baseButton
text: "Back"

onClicked: {
// Emit the new clicked signal here:
backButton.clicked();
}
}
}

window :
Window {
id: window
visible: true

BackButton {
// Respond to the signal here.
onClicked: window.close();
}
}

这提供了将来以其他方式使用自定义 BackButton 类型的灵活性。

关于qt - 无法使用按钮 : QML 关闭窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30109826/

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