1

There's a difference in behaviour between QTableView and QTreeView when it comes to selecting multiple items by dragging the mouse.

In a QTreeView you can start outside the populated area and drag over the items, and they will be selected. In a QTableView this is not not possible. In order to drag and select items you have to start dragging on an item.

Is it possible to make a QTableView behave like a QTreeView in this aspect?

Here's a little example where you can experience the difference:

from PyQt6.QtCore import QAbstractTableModel, Qt
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QTableView, QTreeView


class TModel(QAbstractTableModel):
    def __init__(self):
        super().__init__()
        self.d = [[1, 2], [3, 4], [5, 6]]

    def columnCount(self, parent=None):
        return 2

    def rowCount(self, parent=None):
        return len(self.d)

    def data(self, index, role=1):
        if role == Qt.ItemDataRole.DisplayRole:
            return self.d[index.row()][index.column()]
        return None


class Window(QWidget):
    def __init__(self):
        super().__init__()
        view1 = QTreeView()
        view2 = QTableView()
        for v in (view1, view2):
            v.setSelectionMode(v.SelectionMode.ExtendedSelection)
        model = TModel()
        view1.setModel(model)
        view2.setModel(model)

        lay = QVBoxLayout(self)
        lay.addWidget(view1)
        lay.addWidget(view2)


app = QApplication([])
win = Window()
win.show()
app.exec()
5
  • 1
    QRubberBand and setSelection() are your friends ;-) Commented Sep 12 at 19:33
  • I was hoping something more high level would be possible. Implementing this from scratch feels like too much work for something that's merely a minor annoyance. Commented Sep 14 at 11:29
  • I would be interested in knowing why the difference is there to begin with, so if someone knows something about that, please share. Commented Sep 14 at 11:33
  • I believe that the difference in behavior is simply based on the most common usage for the view type, at least for the period in which the behavior was defined. In reality, overriding that isn't that difficult: the problem is caused by the fact that QTableView only accepts the selection if the given rect contains valid indexes, therefore you just need to "clip" it to the actual contents: try to override setSelection() and add rect &= QRect(0, 0, self.horizontalHeader().length(), self.verticalHeader().length()) before calling the default implementation. Commented Sep 14 at 15:17
  • Note: the QRubberBand suggestion above is only to provide a visual cue for the selection, it's not necessary for the selection implementation. Commented Sep 14 at 18:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.