0

I'm trying to add color to the checkboxes in a QTreeView, like so

Colored checkboxes

I switched from a QStyledItemDelegate to a QItemDelegate to have a better control on the drawing process, mostly for the drawCheck method. I can't get the painter to draw the colors I want though. Using css, I succeeded in changing the color of all checkboxes, but I need to choose the colors depending on some properties (unimportant here). Modifying the QStyle of the QTreeView may work but I will have the same "unique color" problem.

Modifying the painter or the option parameters do nothing.

painter->setPen(aRandomColor);
auto coloredOption = option;
coloredOption.backgroundBrush.setColor(aRandomColor);
QItemDelegate::drawCheck(painter, coloredOption, rect, state);

I understand that I can handle the complete drawing process in my own drawCheck without calling QItemDelegate::drawCheck

const auto color = wathever;
draw square outline using color
draw filled square using color
// Do NOT call QItemDelegate::drawCheck(...)

but this will force an uniform feel on all OSes. Is there no way to ask for a background color? Like... paint what you were going to paint, but do it using {color}"?

Eddy Alleman
  • 1,096
  • 2
  • 10
  • 21
Nil
  • 2,345
  • 1
  • 26
  • 33
  • Please, provide your implementation of the delegate – Dimitry Ernot Jul 18 '19 at 13:52
  • There's no need. My `QItemDelegate` is uselessly complex because I also draw icons. There's no interesting part in it relating to this question. Only a empty `drawCheck` implementation where I tested my ideas. I know it works because I was able to change the checkbox state. – Nil Jul 18 '19 at 15:29
  • It would be more efficient ifI could copy and paste your code. The time I'll spend to setup a whole new project could be used for other questions – Dimitry Ernot Jul 18 '19 at 15:42
  • You could try setting the `palette` member of `coloredOption` rather than setting the `backgroundBrush`.property. Just guessing. – G.M. Jul 18 '19 at 16:54
  • @G.M. There's no `palette` member in `QStyleOptionViewItem`. Am I missing something? – Nil Jul 18 '19 at 17:06
  • 1
    It's a member of the [`QStyleOption`](https://doc.qt.io/qt-5/qstyleoption.html#palette-var) base class. – G.M. Jul 18 '19 at 17:09
  • I set the colors of the palette with all `QPalette::ColorRole` to `Qt::red` and it's doing nothing. It looks like it's not used when drawing the checkbox. – Nil Jul 18 '19 at 17:36

1 Answers1

0

I tested all the members I could find, to no avail. So I did what I didn't want to do: handle the drawing process myself.

void ActionsItemDelegate::drawCheck(QPainter *painter, ...) {
    if (thisCheckboxNeedsColoring) {
        painter->setPen(dynamicColor);
#ifdef __APPLE__
        painter->setRenderHint(QPainter::Antialiasing);
        QPainterPath path;
        path.addRoundedRect(QRectF(rect.adjusted(3, 3, -4, -3)), 1.0, 1.0);
        painter->fillPath(path, color);
        painter->drawPath(path);
        if (state != Qt::Unchecked)
        {
          painter->fillRect(rect.adjusted(5, 8, -6, -8), Qt::white);
        }
#else
        painter->drawRect(rect.adjusted(0, 0, -1, -1));
        if (state != Qt::Unchecked)
        {
          painter->fillRect(rect.adjusted(3, 3, -3, -3), color);
        }
#endif
    } else {
        QItemDelegate::drawCheck(painter, option, rect, state);
    }
}

Of course, this doesn't take into account the styles or themes that Windows, Linux and macOS provide. I have no solution to this problem. I'm still open to better solutions!

Nil
  • 2,345
  • 1
  • 26
  • 33