我有以下qml文件,main.qml創建TestWindow.qml元素。QML與直放站之間的信號
我想將TestWindow.qml中的信號(單擊mySignalToMainWindow())按鈕連接到main.qml中的函數testConnection()。
main.qml
Window {
id: _component
property int instances: 3
width: 200
height: Screen.height/2
visible: true
Component.onCompleted: {
x = 40
y = 40
}
Repeater {
id: _windows
model: instances
TestWindow {
index: model.index
leftOffset: _component.width
}
}
Column {
Repeater {
model: instances
Button {
text: "Window " + index
onClicked:{ _windows.itemAt(index).window.raise();
}
}
}
}
function testConnection(){console.log("Subwindow To Main Window")}
}
而且TestWindow.qml:
Item {
id: _windowItem
property int index
property int leftOffset
property alias window: _window
signal mySignalToMainWindow()
Window {
id: _window
visible: true
title: "SubWindowText " + index
Component.onCompleted: {
x = leftOffset
y = 40
width = Screen.width - leftOffset
height = Screen.height/2
}
Text {
id: windowText
text: qsTr("SubWindowText")
}
Button {
text: "SubWindow " + index
onClicked: {console.log("TestWindow::Button onClicked "+_window);
_windowItem.mySignalToMainWindow();
}
}
}
}
我測試了這兩種:
How to bind to a signal from a delegate component within a ListView in QML 和 How to access dynamically/randomly loaded Repeater items in QML?
沒有成功。 那麼,該怎麼做?
謝謝