【问题标题】:QML connect signal from the main component to the child componentsQML 将信号从主组件连接到子组件
【发布时间】:2017-08-04 12:51:27
【问题描述】:

我有一个调用qt_update_values() 到主QML 组件的Qt 应用程序。我想将新值发送给特定的代表。如何将主组件中的update_values() 连接到由另一个qml 中定义的特定子组件接收?

我已经尝试在孩子中定义连接,但我不确定我需要定义什么目标...

main.qml 我有类似的东西:

...
signal update_values(new_values)

function qt_update_values(newValues){
     update_values(newValues);
}

Repeater {
     id:idRepeater
     model: 3

     Rectangle {
         id:example

         Text{ text: "hello"}
         ...

         AnotherComponent {name: "name", othervariables: "others"}
     }
}
...

然后在AnotherComponent.qml 我有:

...
signal update_values_child(new_values)

function onUpdate_values(newValues){
     textid = newValues;
}

Text{ id:textid}
...

【问题讨论】:

    标签: qml connect signal-handling


    【解决方案1】:

    您不会从父节点连接到主节点,而是像这样:

    ...
    id: idOfTheParent  // <=== THIS IS IMPORTANT
    signal update_values(new_values)
    
    function qt_update_values(newValues){
         update_values(newValues);
    }
    
    Repeater {
        id:idRepeater
        model: 3
    
        Rectangle {
            id:example
    
            Text{ text: "hello"}
            ...
    
            AnotherComponent {
                id: idOfAnotherComponent // This ID is only available in the 
                                         // scope of the Component
                                         // that will be instantiated by the
                                         // Repeater, i.e. children of the Rectangle
                name: "name"
                othervariables: "others"
            }
            Connections {
                target: idOfTheParent
                onUpdate_values: idOfAnotherComponent.dosomethingWith(new_values)
            }
        }
    }
    ...
    

    您也可以使用signal.connect() 添加新连接

    Repeater {
        model: 10
        delegate: Item { ... }
        onItemAdded: {
            idOfTheParent.update_values.connect(function() { // do what you want })
        }
    }
    

    但如果它只是广播一个新值,声明式的方式是,在你的委托中拥有属性,并将它们绑定到保存变化值的属性:

    ...
    id: idOfTheParent
    property var valueThatWillChange
    
    Repeater {
        model: 10
        delegate: Item {
            property int valueThatShallChangeToo: idOfTheParent.valueThatWillChange
        }
    }
    ...
    

    让所有这些都带有不同的信号。是可能的:

    对于Connections-解决方案,最简单的方法是调用doSomething,前提是它是正确的委托实例:

    // in the delegate
    Connections {
        target: idOfTheParent
        onValue1Updated: if (index === 1) doYourStuff()
        onValue2Updated: if (index === 2) doYourStuff()
        onValue...
    }
    

    但是第二种方法更容易:

    id: idOfTheParent
    Repeater {
        model: 10
        delegate: SomeItem {
            function doSomething() { console.log(index, 'does something')
        }
        onItemAdded: {
            idOfTheParent['value' + index + 'Updated'].connect(item.doSomething)
        }
        onItemRemoved: {
            idOfTheParent['value' + index + 'Updated'].disconnect(item.doSomething)
        }
    }
    

    【讨论】:

    • 谢谢@derM。它实际上不仅仅是更新......我正在尝试第一种方法,我没有收到任何错误,但我在委托中的 dosomethingWith(new_values) 上设置了一个 console.log ,它根本没有显示任何内容。似乎没有调用该函数...
    • 对不起我的错误,我忘了在最后添加参数。有用!谢谢!!! @derM
    • 最后一个问题,有没有办法只连接特定的代表而不是全部(在我的示例中,代表将是一系列动态的图表)。我想将每个图表与不同的 qt_update_value_chart1 或 qt_update_value_chart2... 有可能吗? @derM
    猜你喜欢
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多