Search code examples
qtqtguiqt4.8

Which Class has to be reimplemented for custom Selection in QTableWidget?


QTableWidget consists of several Selection Modi, which are selectable with the method

setSelectionMode (QAbstractItemView::SelectionMode mode)

None of the given modi fits the type of interactive selection i want to have. I want to implement a selection mode where when a user clicks in one table cell, and then shift+clicks in another table cell, the resulting selection is not a sum of columns and rows between these two, but starts from the first clicked cell, goes in read direction along row by row, ending by the second click. I hope i made clear what i want to do.

Do i need to overwrite the QItemSelectionModel or the QTableWidget? Where are the user clicks for selection are processed?


Solution

  • You need to create a descendant of QItemSelectionModel and reimplement select.

    virtual void select(const QModelIndex & index, QItemSelectionModel::SelectionFlags command)
    virtual void select(const QItemSelection & selection, QItemSelectionModel::SelectionFlags command)
    

    When reimplementing select you can call QItemSelectionModel::select with different arguments to achieve the needed result.

    Then assign an instance of the selection model to QTableWidget with setItemSelectionModel.