Change painter in delegate
-
Hi,
I want to change the font when a cell in QTableView is double clicked, while preserving all the other formatting.
I thought I could simply supply custom painter to the default implementation, but it doesn't change anything.void TableViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QStyleOptionViewItem opt{ option }; initStyleOption(&opt, index); if (index.row() == m_doubleclicked_row) { auto my_painter = QPainter{}; my_painter.setPen(Qt:green); QStyledItemDelegate::paint(&my_painter, opt, index); } }
How can I solve this?
-
@SGaist The following renders new text behind the original instead of changing it. Also, is there a way to set just the font color with this method?
if (index.row() == m_doubleclicked_row) { opt.font = QFont{ "Papyrus", 11 }; QStyledItemDelegate::paint(painter, opt, index); }
@krzysieklfc
Hi
I think you have 2 calls to QStyledItemDelegate::paint then.
can you show whole paitnEvent code?
Maybe you just need return in the IF statement so it dont draw twice.To change color, you can change the palette
opt.palette.setColor(QPalette::HighlightedText,Qt::red);
opt.palette.setColor(QPalette::Text,Qt::red); -
Hi
The QStyledItemDelegate will use the FontRole to paint the text so setting
the prior will not help.
Since the painter function is const we cannot set it there so
maybe you could hook up to the cell double click signal and set the models
data for that cell to include a font that is bold
something likeconnect( this, &QListView::doubleClicked, this, [this](const QModelIndex & index) { QFont font(index.data(Qt::FontRole).value<QFont>()); font.setBold(true); model()->setData(index, font, Qt::FontRole) ; });
-
Hi,
Since you already have a copy of
option
why not modify the font variable of it ? -
Hi
Using QStyleOptionViewItem opt{ option }; is a good idea but you then need
to keep of which index that was doubled clicked.Do you need to later turn it back to non bold, you should it stay bold forever after being db clicked ?
-
@SGaist The following renders new text behind the original instead of changing it. Also, is there a way to set just the font color with this method?
if (index.row() == m_doubleclicked_row) { opt.font = QFont{ "Papyrus", 11 }; QStyledItemDelegate::paint(painter, opt, index); }
-
@SGaist The following renders new text behind the original instead of changing it. Also, is there a way to set just the font color with this method?
if (index.row() == m_doubleclicked_row) { opt.font = QFont{ "Papyrus", 11 }; QStyledItemDelegate::paint(painter, opt, index); }
@krzysieklfc
Hi
I think you have 2 calls to QStyledItemDelegate::paint then.
can you show whole paitnEvent code?
Maybe you just need return in the IF statement so it dont draw twice.To change color, you can change the palette
opt.palette.setColor(QPalette::HighlightedText,Qt::red);
opt.palette.setColor(QPalette::Text,Qt::red);