How to select QTableView row with one click

alphanumeric picture alphanumeric · Jan 17, 2015 · Viewed 15.1k times · Source

When one of the QTableView's QModelIndex is clicked I want to select an entire row of the same-row-indexes.

To accomplish this I connect QTableView's clicked signal to a custom viewClicked() method which receives the clicked QModelIndex automatically:

self.tableview=QTableView()
self.tableview.clicked.connect(self.viewClicked)

Inside of viewClicked(self, clickedIndex) I query clickedIndex'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:

enter image description here

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_())

Answer

Nejat picture Nejat · Jan 17, 2015

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.