QTreeView's disabled row background colour is not right in Qt6
-
Hello all,
In Qt6, QTreeView does not show proper background colour for the disabled column only. Also, when this column is selected and hovered, it does not show proper background colour as per my css file. This problem did not appear in Qt5 but now I need to use Qt6 and after trying many things, I am not find able to find a solution.
In the Qt5, the model's flags method for this disabled column is
Qt::ItemIsSelectable but it was not Qt::ItemIsEnabled.Below is the original css file for the QtreeView without any changes which worked in Qt5.
QTreeView { ... &::item { background: rgb(255, 255, 255); //white; &:hover { background: rgba(218,236,240,0.45);} // light blue &:disabled { color: rgb(170,170,170); } //dark grey &:selected { background: rgb(205,230,235); //medium blue; &:disabled { background-color: rgb(220,220,220);} //lightgray &:hover { background-color: rgb(190,223,230); } //light blue } } } Attached the QTreeView picture from Qt5 which is expected in Qt6. Note that the checkbox in that column is a svg image.! ![QT5_QTreeView_DisabledColumn.png](https://ddgobkiprc33d.cloudfront.net/0cf7e391-cf51-4a67-953e-83fff320e4bb.png) From Qt5, this treeview's model has a delegate (subclassed from QStyledItemDelegate) set but the paint method does not have any condition for this disabled column! In Qt6, there is no background colour at all for the disabled column when the row is selected. It seems a Qt6 bug and only when I enable the flag of the disabled column (using Qt::ItemIsEnabled), I get blue background colour when selected (instead of gray) as its not disabled anymore! I did not find any workaround to do in css file to have the expected background colours for the now enabled "disabled" column. Now, in the delegate's paint method, I added a condition for the disabled column. When the row is not selected, I have the checkbox image at the right place. When the row is selected, I have the gray background in the cell but I don't see the checkbox image on top of the painted gray background which is the problem I have now. I tried to draw the checkbox image with QPainter, QIcon, etc but did not find a solution like the Qt5 one. Any help will be highly appreciated. void ModelDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { ... else if (index.column() == Model::DisabledColumn) { const QString image = u":/css/ico/checkbox_empty_disabled.svg"_s; QIcon icon(image); QStyleOptionViewItem new_option(option); if (!icon.isNull()) { //This adds the checkbox as expected at the right position new_option.widget->style()->drawItemPixmap(painter, new_option.rect, Qt::AlignLeft | Qt::AlignVCenter, icon.pixmap(16, 16)); } if (new_option.state & QStyle::State_Selected) { painter->fillRect(new_option.rect, QBrush(QColor(220, 220, 220))); //The below image does not appear and this cell only has gray colour. new_option.widget->style()->drawItemPixmap(painter, new_option.rect, Qt::AlignLeft | Qt::AlignVCenter, icon.pixmap(16, 16)); } return; } }
-
@BadMan2016
Which version of Qt6 are you referring to?
Please provide a small reproducer example.
Maybe also screenshots to point out the differences.Platform theming has changed quite a bit from Qt5 to Qt6, especially since 6.5. It may be related to one or more of these changes.
-
Thanks for your reply.
I am using Qt6.2 under Windows 11. I already attached images in my first message but not sure why it did not get displayed.
Below is the image from Qt5 which is good one and the behaviour I would like to have in Qt6:Below is the image (bad one) under Qt6.2
What I want?
The checkbox is a svg image in the column of QtreeView. By default the checkbox is shown in that column without hovering over the row or when the row is not selected.
I just want to change or have different the background colours around the checkbox image when the row is selected (gray colour) and when the mouse is hovered on the row (light blue colour) without redrawing the checkbox image.I already have a paint delegate method and I have added an if-else condition for this specific column. When the row is selected, "painter->fillRect" paints the cell with gray colour and the checkbox image is not seen anymore and the next "drawItemPixmap"
does not draw or make the checkbox visible.void ModelDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { ..... else if (index.column() == Model::DisabledColumn) { if (new_option.state & QStyle::State_Selected) { painter->fillRect(new_option.rect, QBrush(QColor::gray)); //The below image does not appear and this cell only has gray colour. new_option.widget->style()->drawItemPixmap(painter, new_option.rect, Qt::AlignLeft | Qt::AlignVCenter, icon.pixmap(16, 16)); } //end of if return; } //end of else-if }
-
@BadMan2016
Thanks. Can you try if 6.5 produces a better result, please? -
@Axel-Spoerl
My bad! Actually, Qt 6.5.1 is being used and not 6.2