Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

TableView rowDelegate highlight problem



  • Hello!

    I would like to have a TableView which can show a contextMenu if the user clicks the right mouse button on an item. So I wrote my own rowDelegate with a MouseArea. Selecting and showing the menu works fine, but the highlighting does not change if I select another item. What I mean is this: I select the first item in the TableView. The item is highlighted and everything is fine. But then I select a second item and the highlight of the first item stays, so both items are highlighted (but only one item is selected). I have spent a surprising amount of time on this, at least so it seems, simple problem. What am I doing wrong? Thank you for your help.

    @TableView {
    id: tableView

    anchors.fill: parent
    anchors.margins: 2
    frameVisible: false
    alternatingRowColors: false
    backgroundVisible: false

    model: myModel

    TableViewColumn {
    role: "name"
    title: qsTr("Name")
    }
    TableViewColumn {
    role: "value"
    title: qsTr("Value")
    }
    rowDelegate: Rectangle {
    width: tableView.width
    color: styleData.selected ? "blue" : "transparent"

    MouseArea {
      propagateComposedEvents: true
      anchors.fill: parent
      acceptedButtons: Qt.LeftButton | Qt.RightButton
      onClicked: (mouse.button === Qt.RightButton) ? contextMenu.popup() :  tableView.selection.select(styleData.row)
    }
    

    }
    }@



  • Well, it turned out it was a simple problem after all. You have to call selection.clear() before calling selection.select.


Log in to reply