【问题标题】:How to connect a signal from an object created by stackview.push in QML如何连接来自 QML 中 stackview.push 创建的对象的信号
【发布时间】:2018-10-30 01:53:35
【问题描述】:

我在 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)
             })
        })
    }
}

【问题讨论】:

  • 您好,先生,我对其进行了编辑以包含使用它的部分。

标签: qt qml


【解决方案1】:

在 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(){ ...})

在第二种形式中,它的信号到信号连接..不要使用函数。

Connecting Signals to Methods and Signals

【讨论】:

    【解决方案2】:

    好吧,要访问推送的元素,您必须使用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)
                 })
            })
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      相关资源
      最近更新 更多