Teacher Mik
Teacher Mik

Reputation: 176

Python QTableView | How to select multiple rows by click without holding down Ctrl?

I am designing a GUI with a QTableView in which one would have to select multiple rows when needed. This means that the first click will select the row and the second click will unselect the row. So there are two specific questions that I have:

  1. How to convert a cell selection to a row selection?
  2. How to convert a accomplish multiple selections without holding down Ctrl?

Upvotes: 2

Views: 1018

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

To select rows instead of items you must set them as selection behavior in QAbstractItemView::SelectRows, and if you want items to be selected without pressing any key you must set the selection mode in QAbstractItemView::MultiSelection:

import sys
from PyQt5 import QtGui, QtWidgets

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    model = QtGui.QStandardItemModel(10, 5)
    w = QtWidgets.QTableView()
    w.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
    w.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
    w.setModel(model)
    w.show()
    sys.exit(app.exec_())

Upvotes: 2

Related Questions