我在QML中有一个对象,该对象将使用stackview的.push
函数创建,如下所示:
stackview.push(myObject)
,我想连接到myObject的信号。在对象内部,我有一个信号。
这是我的连接方式:
myObject.onMySignal.connect(function (){
console.log("Signal Recieved")
})
问题是我没有收到信号。因此,我得出的结论是我做错了。请帮忙。
下面是我完成解决方案的代码部分:
StackView {
width: parent.width
id: stackView
initialItem:HomeForm{height: root.height-toolbar.height ; width: root.width}
anchors.fill: parent
Component.onCompleted: {
initialItem.onSubjectClicked.connect(function(index){
stackView.push("qrc:/AttendanceList.qml")
AttendanceList.onSessionItemClicked.connect(function (data){
console.log("Attendance click registered with index: " + data)
})
})
}
}
答案 0 :(得分:0)
在QML中,connect()
是信号方法,因此您可以按原样使用它;将signal
连接到function
或信号到信号。
您在connect()
中使用信号 handler ,这是绝对错误的:
myObject.onMySignal.connect(function (){}) // Wrong
您必须在connect()
myObject.mySignal.connect(function (){}) // Correct signal to function
因此,您可以将其用于发出这样的信号:
AttendanceList.sessionItemClicked.connect(mySignal)
或发出如下信号:
AttendanceList.sessionItemClicked.connect(function(){ ...})
在第二种形式中,其信号到信号连接..不使用功能。
答案 1 :(得分:0)
好吧,要访问被压入的元素,您必须使用currentItem
作为参考。请注意也要注意@Mohammad Kanan的要点。
StackView {
width: parent.width
id: stackView
initialItem:HomeForm{height: root.height-toolbar.height ; width: root.width}
anchors.fill: parent
Component.onCompleted: {
initialItem.subjectClicked.connect(function(index){
stackView.push("qrc:/AttendanceList.qml")
stackView.currentItem.sessionItemClicked.connect(function (data){
console.log("Attendance click registered with index: " + data)
})
})
}
}