16

I'm using the QTableView class with this model:

class PaletteTableModel(QtCore.QAbstractTableModel):
    def __init__(self,colors = [[]],headers =[],parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self.__colors=colors
        self._headers=headers

    def rowCount(self,parent): 
        return len(self.__colors)
    
    def columnCount(self,parent):
        return 6        
    
    def headerData(self,section,orientation,role):
        if role==QtCore.Qt.DisplayRole:
            if orientation==QtCore.Qt.Horizontal:
                return self._headers[section]  
            else:
                return QtCore.QString("Credit %1").arg(section+1) 
            
    def data(self,index,role):
        if role==QtCore.Qt.ToolTipRole:
            row=index.row
            return "Crédit"
        if role==QtCore.Qt.EditRole:
            row=index.row()
            column=index.column()
            return self.__colors[row][column]
        if role==QtCore.Qt.DisplayRole:
            row=index.row()
            column=index.column()
            value=self.__colors[row][column]
            return value  

    def setData(self,index,value,role=QtCore.Qt.EditRole):
        if role==QtCore.Qt.EditRole:
            row =index.row()
            column=index.column()
            ch=(value)
            
            self.__colors[row][column]=ch
            self.dataChanged.emit(index,index)
            return True
                
    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled|QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsSelectable

I want to retrieve the selected row of this table-view. (And I want to have Python code, if possible).

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Haussem ChƏdly
  • 249
  • 2
  • 4
  • 11
  • possible duplicate of [how to get selected rows in QTableView](http://stackoverflow.com/questions/5927499/how-to-get-selected-rows-in-qtableview) – Oliver Mar 22 '14 at 17:50

3 Answers3

34

It depends what you mean by "the selected row". By default, a QTableView has its selection mode set to ExtendedSelection, and its selection behavior set to SelectItems. This means that several individual table cells in different rows and columns can be selected at the same time. So which one should count as "the" selected row?

The selection model of the table-view has a selectedRows method which will return a list of indexes for the rows where all the colums are selected (i.e. as it is when you click on the header section for a row):

    indexes = table.selectionModel().selectedRows()
    for index in sorted(indexes):
        print('Row %d is selected' % index.row())

However, if you want to get all the rows where at least one cell is selected, you can use the selectedIndexes method:

    rows = sorted(set(index.row() for index in
                      self.table.selectedIndexes()))
    for row in rows:
        print('Row %d is selected' % row)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • You can also ensure only one row is selected by table.setSelectionBehavior(QAbstractItemView.SelectRows) and table.setSelectionMode(QAbstractItemView.SingleSelection) – Pepv Dec 16 '22 at 14:39
8

One of the way's you can use to retrieve the selected rows is:

tableView = QtGui.QTableView()
tableModel = PaletteTableModel()   
tableView.setModel(tableModel)

x = tableView.selectedIndexes ()

"tableView.selectedIndexes ()" returns a list-of-QModelIndex's

thecreator232
  • 2,145
  • 1
  • 36
  • 51
7

In my case I use this action into a function that is called in doubleClicked event something like this: I add this line code into the init function

self.tableSusAmigos.doubleClicked.connect(self.doubleClicked_table)

After that I declared doubleClicked_table like this:

def doubleClicked_table(self):
    index = self.tableSusAmigos.selectedIndexes()[0]
    id_us = int(self.tableSusAmigos.model().data(index).toString())
    print ("index : " + str(id_us)) 

In this case I show an id (integer) that it's in the first column (that's the reason the number 0 in selectedIndexes()[0])

GSandro_Strongs
  • 773
  • 3
  • 11
  • 24