【问题标题】:QML TableView: how to bind to selection.onSelectionChanged()QML TableView:如何绑定到 selection.onSelectionChanged()
【发布时间】:2015-08-30 00:46:17
【问题描述】:

我知道如何绑定到像 onClicked 这样的根 TableView 信号,但由于 onSelectionChanged 是其 selection 属性的信号,我不确定如何绑定到它。

我确信我可以使用Connections 来绑定它,但是下面的代码不起作用:

编辑Connections确实有效,问题是不相关的错误(见我自己的答案)

    TextArea {
        id: messageDetailTextArea
        readOnly: true
        font: messageListDialog.font

        function update() {
            var selectionText = ''
            messagesTable.selection.forEach(function(rowIndex) {
                var row = model.get(rowIndex)
                if (row && row.message) selectionText += row.message
            })
            text = selectionText
        }

        Connections {
            target: messagesTable.selection
            onSelectionChanged: update()
        }
    }

但不幸的是,当我单击表格中的一行时,TextArea 不会更新。如何响应选择更改?

【问题讨论】:

    标签: qt qml tableview qt5 qt-signals


    【解决方案1】:

    啊,所以我没有 model 和对 update() 的调用正确限定。这有效:

        TextArea {
            id: messageDetailTextArea
            readOnly: true
            font: messageListDialog.font
    
            function update() {
                var selectionText = ''
                messagesTable.selection.forEach(function(rowIndex) {
                    var row = messagesTable.model.get(rowIndex)
                    if (row && row.message) selectionText += row.message
                })
                text = selectionText
            }
    
            Connections {
                target: messagesTable.selection
                onSelectionChanged: messageDetailTextArea.update()
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多