opa*_*tut 14 window qobject 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);
}
}
Run Code Online (Sandbox Code Playgroud)
我试图避免编写一个Q_OBJECT类只是为了在新的内部实现新窗口QmlApplicationViewer.
Kkn*_*knd 31
您可以使用Qt.createComponent来完成.示例(使用Qt 5.3):
main.qml
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
id: root
width: 200; height: 200
visible: true
Button {
anchors.centerIn: parent
text: qsTr("Click me")
onClicked: {
var component = Qt.createComponent("Child.qml")
var window = component.createObject(root)
window.show()
}
}
}
Run Code Online (Sandbox Code Playgroud)
Child.qml
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
id: root
width: 100; height: 100
Text {
anchors.centerIn: parent
text: qsTr("Hello World.")
}
}
Run Code Online (Sandbox Code Playgroud)