How to draw row selection if item delegate has been subclassed
-
Hey everyone,
I've built a custom QStyledItemDelegate with a paint function so I can display some columns in a different way. However, this has eliminated any ability to select anything in the table, regardless of the QTableView settings (ie SingleSelection, SelectRows). This is the code for my delegate:
class RefractOverUnderDelegate(QStyledItemDelegate): def paint(self, painter: QPainter, option, index: QModelIndex): combine_columns_pairs = { 2:None, 3:5, 7:8, 24:36, 25:37, 26:38, 27:39, 28:40, 29:41, 30:42, 31:43, 32:44, 33:45, 34:46, 35:47, 65:66 } if index.column() in combine_columns_pairs.keys(): row = index.row() top_col = index.column() bot_col = combine_columns_pairs[top_col] if combine_columns_pairs[top_col] == None: data_bot = "" else: data_bot = index.model().data(index.sibling(row, bot_col), 0) data_top = index.model().data(index.sibling(row, top_col), 0) data = str(data_top) + "\n" + str(data_bot) painter.drawText(option.rect, Qt.AlignCenter, data) else: super().paint(painter, option, index)and this produces this kind of table:

I realize my delegate is kinda janky but I couldn't figure out a better way to do it, and I need to move on with the project.Any recommendations for doing row selection?
Regards,
Jude -
Hi,
From the looks of it, your main issue is that you are not painting the selection rectangle in your delegate.
-
Hi,
From the looks of it, your main issue is that you are not painting the selection rectangle in your delegate.
@SGaist thanks for the reply. How would I go about painting the selection?
-
As silly as it may sound: paint a transparent rectangle after you are done painting the rest of your cell.
Use the [state from your QStyleOptionViewItem to know whether you need to raw the focus rectangle.
-
As silly as it may sound: paint a transparent rectangle after you are done painting the rest of your cell.
Use the [state from your QStyleOptionViewItem to know whether you need to raw the focus rectangle.
@SGaist thanks that worked!
For anyone that finds this thread, this is the code I used (courtesy of chatGPT actually):
class RefractOverUnderDelegate(QStyledItemDelegate): def paint(self, painter: QPainter, option, index: QModelIndex): if option.state & QStyle.State_Selected: colour = option.palette.highlight().color() painter.fillRect(option.rect, colour)I found I needed to highlight (fillRect) the cell and then paint the text over the highlight.
-
@SGaist thanks that worked!
For anyone that finds this thread, this is the code I used (courtesy of chatGPT actually):
class RefractOverUnderDelegate(QStyledItemDelegate): def paint(self, painter: QPainter, option, index: QModelIndex): if option.state & QStyle.State_Selected: colour = option.palette.highlight().color() painter.fillRect(option.rect, colour)I found I needed to highlight (fillRect) the cell and then paint the text over the highlight.
@judethedude said in How to draw row selection if item delegate has been subclassed:
(courtesy of chatGPT actually)
OOI, do you know exactly how you phrased your question to ChatGPT?