PySide6 - Color specific row with QTableView QAbstractTableModel and Pandas

109 views Asked by At

This is the first application I have created with Python QT so apologies if I have missed something really fundamental.

The following table is populated via a QAbstractTableModel linked to a Pandas Framework. I have an PlayerIDX (e.g. 30, who in the example is running 25th) and want that row to be highlighted. At the moment I have the following function:

def highlight_player_row(self, row):
    for column in range(0,self.columnCount()):
        ix = self.index(row, column)
        print(ix)
        self.colors[(row, column)] = QBrush(Qt.darkCyan)
        self.dataChanged.emit(ix, ix, (Qt.BackgroundRole,))

which called with highlight_player_row(30) results in the following:

enter image description here

Rather than row 24/31 being highlighted I would like row 30/25 (marked in yellow).

I don't know if it makes any difference but I am sorting the dataframe prior with:

self.idx_data = idx_df.sort_values(by=['CarIdxPosition'])

Do I need to go back to where the dataframe is formed from the incoming data and flagged?

1

There are 1 answers

1
Timeless On BEST ANSWER

IIUC, try get_loc to return the physical position of the sorted DataFrame's index :

def highlight_player_row(self, row):
    for column in range(0, self.columnCount()):
        ix = self.index(self.idx_data.index.get_loc(row), column) # << here
        self.colors[(row, column)] = QBrush(Qt.darkCyan)
        self.dataChanged.emit(ix, ix, (Qt.BackgroundRole,))

Or ignore the index when sorting and then highlight_player_row(24) :

self.idx_data = idx_df.sort_values(by=['CarIdxPosition'], ignore_index=True)