Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How can i enable selection on TableView in QML Qt6.3?

How can i enable selection on TableView in QML Qt6.3?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    I'm having trouble in enabling selection on QML model/view in Qt6.3.
    In order to understand the behaviour i took the example given by Qt on the documentation of Table View about selection (see here).
    This is my whole main.qml file:

    import QtQuick 
    import QtQuick.Controls
    import Qt.labs.qmlmodels
    
    
    Window {
    id: mainWin
    visible: true
    width: 640
    height: 480
    title: qsTr("QML Test")
    
    TableView {
        id: tableView
        anchors.fill: parent
        clip: true
    
        model: TableModel {
            TableModelColumn { display: "name" }
            rows: [ { "name": "Harry" }, { "name": "Hedwig" } ]
        }
    
        selectionModel: ItemSelectionModel {
            model: tableView.model
        }
    
        delegate: Rectangle {
            implicitWidth: 100
            implicitHeight: 30
            color: selected ? "blue" : "lightgray"
    
            required property bool selected
    
            Text { text: display }
        }
    }
    
    }
    

    and a very basic main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
    #if defined(Q_OS_WIN)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    The application start, and the table is shown with proper data, but rows are not selectable.
    What am i missing?

    Thank you.
    Michele

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @belox86 said in How can i enable selection on TableView in QML Qt6.3?:

      What am i missing?

      The fact that TableView doesn't provide the UI to select rows automatically.

      If you want to be able to select cells by long click or drag, you can use a SelectionRectangle

      If you want to have checkboxes to select rows, you can add this in your tableView:

      leftMargin: checkColumn.width
      
      VerticalHeaderView {
          id: checkColumn
          parent: tableView
          syncView: tableView
          selectionModel: tableView.selectionModel
          boundsBehavior: tableView.boundsBehavior
          delegate: CheckBox {
              required property int row
              required property bool selected
              checked: selected
              onToggled: tableView.selectionModel.select(tableView.model.index(row, 0), ItemSelectionModel.Toggle | ItemSelectionModel.Rows)
          }
      }
      
      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #3

        ok, thank you very much.
        that's no where mentioned in TableView documentation.

        So i went back with my original code, which actually was a TreeView in which i want to highlight the selected row.
        The model is a c++ implemented QAbstractItemModel which is set to the qml context from the main.cpp

        import QtQuick 
        import QtQuick.Controls
        
        Window {
            id: mainWin
            visible: true
            width: 640
            height: 480
            title: qsTr("QML Test")
        
            TreeView {
                id: treeView
                anchors.fill: parent
                model: projectModel
            
        
                selectionModel: ItemSelectionModel {
                    model: treeView.model
                }
            
            
                delegate: TreeViewDelegate {
                    id: treeDelegate
                
                    required property bool selected
                    required property int row
                    
                    background: Rectangle {
                        opacity: treeDelegate.selected ? 1 : 0
                        color: "red"
                    }
                    onClicked: {
                        treeView.selectionModel.select(treeView.modelIndex(treeDelegate.row, 0),  ItemSelectionModel.Toggle | ItemSelectionModel.Rows)
                        console.log("clicked:", treeView.model.data(treeView.modelIndex(treeDelegate.row, 0)))
                        console.log("Selection model selected? ", treeView.selectionModel.isSelected(treeView.modelIndex(treeDelegate.row, 0)))
                        console.log("Delegate selected? ", treeDelegate.selected)
                    }
              
                }
        }
        

        The selection works perfecly on 0-depth items for which the row become red.
        the log on first click say

        qml: clicked: Parent1
        qml: Selection model selected?  true
        qml: Delegate selected?  true
        

        and on the second (toggle):

        qml: clicked: Parent1
        qml: Selection model selected?  false
        qml: Delegate selected?  false
        

        So far so good.
        Unfortunatly if i click on 1-depth item the row does not color and i get this log:

        qml: clicked:  Child1
        qml: Selection model selected?  true
        qml: Delegate selected?  false
        

        So the clicked item is properly indexed (i retrive the proper dysplayRole with the used index), the selection model actually select it BUT the delegate does not update the selected properties...
        any idea on what's wrong?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          sry, i will open a dedicate topic and mark this as resolved.
          Thank you

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved