4
votes

Is there a class or method in Qt to get properties like "border-color", "border-style", ...

This may be usefull to create and paint own (derived) controls based on currently selected style sheets.

Example:

QPushButton {
    border: 3px solid red;
    background: blue;
    margin: 5px;
    padding: 10px;
}

The code:

class QPushButtonCircle : public QPushButton {
};

QPushButtonCircle would be a button that is circular. Even the border is circular. The border should 3 pixels width, solid and colored red. The background should be colored blue.

I din't find any way expect drawing entierly in paintEvent and ignoring any style sheets. But there must be a better way.

The best would be something like:

QCssStyle cssStyle = widget->...->getCssStyle();

QPen border = cssStyle->border(QCssStyle::Top);
QBrush background = cssStyle->background();
QMargins margins = cssStyle->margins();
QMargins padding = cssStyle->padding();
...

This would allow us to draw our own controls like in:

QStylePainter p(this);

QRect r = rect();
r.adjust(cssStyle->margins().left(), ...);

p.setPen(cssStyle->border());
p.setBrush(cssStyle->brush());
p.drawEllipse(r);

r.adjust(cssStyle->padding().left(), ...);
p.drawText(r, ...);

A better solution would be a method to draw things like with QStyle::drawControl, QStyle::drawPrimitive... but with respecting a QPainterPath (or simpler primitives) instead of assuming rectangular controls.

What is the best way to create owner drawn controls in Qt with derived colors from style sheets?

1
Whoa, that would be a really great thing to have, but there is no. Probably you can implement it as a wrapper over QString css property by parsing it with some parser. But without touching the Qt internals it is difficult to handle this: widget's stylesheet property is not the complete computed style: parents and qApp also influence. Probably a reason for feature request to Qt devs...NIA

1 Answers

0
votes

I din't find any way expect drawing entierly in paintEvent and ignoring any style sheets. But there must be a better way.

Widgets in Qt are rectangles, and cannot have any other shape. Take a look at the box model. So any other way will be as tricky as the one you are trying to use.

There is a suggestion using QGraphicView.

EDIT:

I have never tried myself, but you can try to set a mask on top of the button with the shape you want, either using a QBitmap or a QRegion. The other css elements (color, etc..)can be set up by updating the stylesheet. This is a much less pain in the you know what solution if you can make it work, because you don't need to deal with custom paint events.