Painting Qt controls in paintEvent with style properties

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

This can be useful for creating and drawing your own (derived) controls based on the currently selected style sheets.

Example :

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

      

Code:

class QPushButtonCircle : public QPushButton {
};

      

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

I won't find any way to wait for painting in paintEvent and ignore any stylesheets. 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 will allow us to draw our own controls, for example:

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, ...);

      

The best solution is a method of drawing objects, such as QStyle::drawControl

, QStyle::drawPrimitive

... but considering QPainterPath (or more simple primitives) instead of adopting the rectangular controls.

What is the best way to create an owner of drawn controls in Qt with derived colors from stylesheets?

+3


source to share


1 answer


I won't find any way to wait for painting in paintEvent and ignore any stylesheets. But there must be a better way.

Qt widgets are rectangles and cannot have any other shape. Look at the box. Thus, any other way will be as difficult as the one you are trying to use.

There is a suggestion using QGraphicView .



EDIT:

I've never tried it myself, but you can try to set a mask on top of the button with the shape you want , either using QBitmap

or QRegion

. Other elements css

(color, etc.) can be customized by updating the style sheet. It's much less of a pain that you know which solution if you can get it to work, because you don't have to deal with custom paint events.

0


source







All Articles