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

Does TableView supports items selection?



  • Is there a way to make TableView select row and access selected row index?
    QT 5.13, QuickControls 2.



  • @dmitriano
    hi see this qt Blog comment, and it looks like there is still not selection aftera quick search

    Richard Moe Gustavsen
    August 31, 2018 at 10:51

    1. TableView itself does not (yet) contain any selection support. For now, you would need to store which rows/columns/items that are selected in a your own data structure (or e.g add a model role “selected” to you data model). Then bind that information/role to a selected state (e.g bgcolor) inside the delegate to show it as selected.
    2. rowSpan and colSpan are not available in this first release.
    3. Even if TableView itself has a fixed width of e.g 750px, the content view inside it that you flick around, can be of any size. And it’s always resized to fit the size of the table. So (normally) there will be no extra left-over space at the end that the last column could fill. But it might still make sense for tables that are smaller than the TableView size itself, or if you set a custom contentWidth. I’ll make a note.

    in QtQuick.Controls 1.4 there is a selection property



  • @lelev hi!
    If I have "selected" role (of type bool) in my model, for example, how do I set it to true when the user clicks on a cell (or row)? For example, if I add MouseArea to the delegate what should I do in its onClicked handler? Is there a common technique for updating the model from the delegate?



  • @dmitriano hi,
    so what are you using now controls 1 or 2 ?

    @dmitriano said in Does TableView supports items selection?:

    Is there a common technique for updating the model from the delegate?

    it depends on your model, is it qml listmodel or QAbstractTableModel defined in c++ ?



  • @dmitriano said in Does TableView supports items selection?:

    @lelev hi!
    If I have "selected" role (of type bool) in my model, for example, how do I set it to true when the user clicks on a cell (or row)? For example, if I add MouseArea to the delegate what should I do in its onClicked handler? Is there a common technique for updating the model from the delegate?

    You can use ItemDelegate for example. If model set properly, then when clicked you can switch your role.
    Example:

    delegate: ItemDelegate {
        ...
        contentItem: ...
        onClicked: model.selected = !model.selected
        ...
        background: Rectangle {
            visible: model.selected
            color: "blue"
            opacity: 0.5
        }
    }
    

Log in to reply