看着问题How can I create a new window from within QML? 我们看到在那里我们可以这样创建一个新窗口:
main.qml
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
id: root
width: 200; height: 200
Button {
anchors.centerIn: parent
text: qsTr("Click me")
onClicked: {
var component = Qt.createComponent("Child.qml")
var window = component.createObject(root)
window.show()
}
}
}
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.")
}
}
但是这种方法似乎无法将变量传递给child.qml,然后在我们在此新Window上提交表单后,将它们放在main.qml上,还是可能吗?我们该怎么办?
关于要传输到Child.qml的变量,我想这样会做:
var b = tabButton.createObject(tabbar, {tabId: tabId, trCtx: tabs[t], tabTitle: c.title,
"font.pixelSize": 14, "font.bold": globals.menu.fontBold,
"font.family": robotoRegular.name})
如果这很好,那么唯一缺少的就是从Child.qml获取值。
答案 0 :(得分:0)
要初始化新对象,可以使用初始化列表或在创建对象后分配新变量。当然,使用初始化列表是最好的解决方案。如果您确实要传递“变量”(例如列表或类似的东西),则主窗口始终可以调用子窗口的功能。
可以使用信号和插槽来归档两个窗口之间的通信。 您可以使用JavaScript或QML进行连接。在示例中,您将找到两种方式。您可以听自己的信号或听财产变化。
main.qml
//========================
//== Includes
import QtQuick 2.3
import QtQuick.Controls 1.2
//========================
//== Base Item
ApplicationWindow {
//========================
//== Properties
property var childWindowHandle: null
id: root
width: 200;
height: 200
visible: true
//========================
//== Childs
Button {
//========================
//== Properties
anchors.centerIn: parent
text: qsTr("Click me")
//========================
//== Functions
function outputNewValue(){
console.log(childWindowHandle.iWillChangeSoon)
}
//========================
//== Connections
onClicked: {
var component = Qt.createComponent("Child.qml")
// Using the "constructor" to initialize the object WHILE creating (no change signals emitted)
root.childWindowHandle = component.createObject(root, { withConstructor: "\\(^_^)/" })
// Check if creation was successfully
if(root.childWindowHandle){
// Change the value AFTER construction. The onAfterConstructorChanged signal get fired
root.childWindowHandle.afterConstructor = "_(._.)_";
// "Listen" on variable change. Every property has a corresponding change signal ("Changed" after name)
root.childWindowHandle.iWillChangeSoonChanged.connect(outputNewValue)
// Just show window...
root.childWindowHandle.show()
}
}
}
Connections{ // <- You can also use "Connections" to listen for changes
//========================
//== Properties
target: root.childWindowHandle ? root.childWindowHandle : null
ignoreUnknownSignals: true // Important, because "childWindowHandle" can be "null"
//========================
//== Connections
onSpecialSignal: console.log("An other signal fired!")
}
}
Child.qml
//========================
//== Includes
import QtQuick 2.3
import QtQuick.Controls 1.2
//========================
//== Base Item
ApplicationWindow {
//========================
//== Properties
property string withConstructor: ""
property string afterConstructor: ""
property string iWillChangeSoon: ""
id: root
width: 100;
height: 100
//========================
//== Signals
signal specialSignal()
//========================
//== Connections
Component.onCompleted: {
console.log("Child Component Completed")
console.log("withConstructor:" + withConstructor)
console.log("afterConstructor:" + afterConstructor)
}
//========================
//== Childs
Text {
//========================
//== Properties
anchors.centerIn: parent
text: qsTr("Hello World.")
}
Timer{
//========================
//== Properties
running: true
interval: 5000
//========================
//== Connections
onTriggered:{
// Change variable
root.iWillChangeSoon = "Yep, I changed ;)"
// Emit our special signal
root.specialSignal();
}
}
}