How to select QTableView row with one click
You can use setSelectionBehavior
function of QTableView
to set the selection behavior to QTableView.SelectRows
:
self.tableview.setSelectionBehavior(QTableView.SelectRows);
Now when you click on an item, the entire row is selected.
alphanumeric
Updated on June 09, 2022Comments
-
alphanumeric about 2 years
When one of the
QTableView
'sQModelIndex
is clicked I want to select an entire row of the same-row-indexes.To accomplish this I connect
QTableView
'sclicked
signal to a customviewClicked()
method which receives the clicked QModelIndex automatically:self.tableview=QTableView() self.tableview.clicked.connect(self.viewClicked)
Inside of
viewClicked(self, clickedIndex)
I queryclickedIndex
's row number, its model and the total number of columns):row=clickedIndex.row() model=clickedIndex.model() columnsTotal=model.columnCount(None)
Finally to select an every index in a row:
for i in range(columnsTotal): self.tableview.selectRow(row)
The problem is it is noticeably slow for Qt to process such an action. I wonder if there is a faster way to select an entire row of indexes when one of the tableview items is clicked:
from PyQt4.QtCore import * from PyQt4.QtGui import * import sys class Model(QAbstractTableModel): def __init__(self, parent=None, *args): QAbstractTableModel.__init__(self, parent, *args) self.items = ['Row0_Column0','Row0_Column1','Row0_Column2'] def flags(self, index): return Qt.ItemIsEnabled | Qt.ItemIsSelectable def rowCount(self, parent): return 1 def columnCount(self, parent): return len(self.items) def data(self, index, role): if not index.isValid(): return QVariant() elif role != Qt.DisplayRole: return QVariant() column=index.column() if column<len(self.items): return QVariant(self.items[column]) else: return QVariant() class MyWindow(QWidget): def __init__(self, *args): QWidget.__init__(self, *args) tablemodel=Model(self) self.tableview=QTableView() self.tableview.setModel(tablemodel) self.tableview.clicked.connect(self.viewClicked) layout = QHBoxLayout(self) layout.addWidget(self.tableview) self.setLayout(layout) def viewClicked(self, clickedIndex): row=clickedIndex.row() model=clickedIndex.model() columnsTotal=model.columnCount(None) for i in range(columnsTotal): self.tableview.selectRow(row) if __name__ == "__main__": app = QApplication(sys.argv) w = MyWindow() w.show() sys.exit(app.exec_())
EDITED LATER: REVISED WORKING CODE Thanks to Nejat! :
from PyQt4.QtCore import * from PyQt4.QtGui import * import sys class Model(QAbstractTableModel): def __init__(self, parent=None, *args): QAbstractTableModel.__init__(self, parent, *args) self.items = ['Row0_Column0','Row0_Column1','Row0_Column2'] def flags(self, index): return Qt.ItemIsEnabled | Qt.ItemIsSelectable def rowCount(self, parent): return 1 def columnCount(self, parent): return len(self.items) def data(self, index, role): if not index.isValid(): return QVariant() elif role != Qt.DisplayRole: return QVariant() column=index.column() if column<len(self.items): return QVariant(self.items[column]) else: return QVariant() class MyWindow(QWidget): def __init__(self, *args): QWidget.__init__(self, *args) tablemodel=Model(self) self.tableview=QTableView() self.tableview.setModel(tablemodel) self.tableview.clicked.connect(self.viewClicked) self.tableview.setSelectionBehavior(QTableView.SelectRows) layout = QHBoxLayout(self) layout.addWidget(self.tableview) self.setLayout(layout) def viewClicked(self, clickedIndex): row=clickedIndex.row() model=clickedIndex.model() if __name__ == "__main__": app = QApplication(sys.argv) w = MyWindow() w.show() sys.exit(app.exec_())
-
alphanumeric over 9 yearsExcellent answer! Thanks a lot!
-
Daniel F. over 5 yearsFor anyone searching this feature for Qt for Python (PySide2) the call goes as follows:
self.tableview.setSelectionBehavior(QAbstractItemView.SelectRows)