如何从 QML 中创建新窗口?
有没有办法创建一个全新的窗口实例,作为 QmlApplication 中主 QML 窗口的子窗口?
// ChildWindow.qml
Rectangle {
id: childWindow
width: 100
height: 100
// stuff
}
// main.qml
Rectangle {
id: window
width: 1000
height: 600
MouseArea {
anchors.fill: parent
onClicked: createAWindow(childWindow);
}
}
我试图避免仅仅为了在新的 QmlApplicationViewer 中实例化新窗口而编写 Q_OBJECT 类。
Is there a way to create a completely new window instance, as a child window of the main QML window in a QmlApplication?
// ChildWindow.qml
Rectangle {
id: childWindow
width: 100
height: 100
// stuff
}
// main.qml
Rectangle {
id: window
width: 1000
height: 600
MouseArea {
anchors.fill: parent
onClicked: createAWindow(childWindow);
}
}
I am trying to avoid writing a Q_OBJECT
class just for instanciating the new window within a new QmlApplicationViewer
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Qt.createComponent 来完成此操作。示例(使用 Qt 5.3):
main.qml
Child.qml
You can do it by using Qt.createComponent. Example (using Qt 5.3):
main.qml
Child.qml
仅使用内置 QML 功能无法创建顶级窗口。
然而,Qt Labs 上有一个名为 桌面组件,其中包含 窗口组件,其中允许您创建新的顶级窗口。
There is no way to create top-level windows using only built-in QML functionality.
However there's a project on Qt Labs called Desktop Components, which among other things contains a Window component, which allows you to create new top-level windows.
使用@Kknd 的方法,我们遇到了子窗口不响应鼠标事件的问题。我们使用的是 Qt 6.6.1。以下内容有效:
main.qml
Child.qml
With @Kknd's approach we had a problem that child window didn't respond to mouse events. We are using Qt 6.6.1. The following worked:
main.qml
Child.qml