Unsolved Failed to change background color of table widget by QItemDelegate derived class
-
void MyTableItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem myOption = option; // not work myOption.palette.setColor(QPalette::All, QPalette::Background, Qt::red); drawBackground(painter, myOption, index); // works myOption.palette.setColor(QPalette::All, QPalette::Text, Qt::red); drawDisplay(painter, myOption, myOption.rect, "Hello"); }
The text color can be changed, but the background color can't be changed.
How to change the background color? (Better in the paint function.)
Thanks. -
QPainter ::fillRect get it done, still confused why QItemDelegate ::drawBackground not work as expected.
-
instead of QPalette::All which doesn't exist in enum QPalette::ColorGroup
you can choose between :QPalette::Disabled QPalette::Active QPalette::Inactive QPalette::Normal
what errors did you get?
-
@Eddy Already tried, no error is reported, just not work.
-
I see in your code you use
QItemDelegate
and
QStyleOptionViewItemAFAIR they don't mix well.
Why are you not using QStyledItemDelegate?
-
@Eddy QStyledItemDelegate doesn't have a method of drawBackground
-
@jronald
QStyledItemDelegate uses Qt::BackgroundRole -
@Eddy Just don't know why QItemDelegate::drawBackground not work