【发布时间】:2017-02-23 09:22:03
【问题描述】:
我有以下 qml 文件,main.qml 创建 TestWindow.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?
没有成功。 那么,该怎么做呢?
谢谢
【问题讨论】:
标签: connection qml signals repeater