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:

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_())
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_())
                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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With