Important: Please read the Qt Code of Conduct -

Clip drawing inside a frame

  • Hi,

    I'm trying to find a solution to my problem since days, without success :(

    What I need is to fill a frame with a color, drawn with QStyle classes. I have a widget, and I paint it in this way:

    QStyleOptionFrameV3 opt;

    QStylePainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    QRect frameRect(this->rect());

    opt.rect = frameRect;
    opt.state = QStyle::State_Sunken;
    opt.lineWidth = 1;
    opt.midLineWidth = 1;
    opt.frameShape = QFrame::StyledPanel;


    Now I need to draw a filled rect (with some transparency), that is a percentage of the full frame rect (just like a progressbar). The problem is that the filled rect "overflows" the frame, painting outside too.
    Progressbar doesn't fit my needs, cause the filled color is changing according to some policies, as long as the value changes.

    I could adjust the filled rect, but I still have problems with the rounded corners.

    Is there a way to clip the drawing inside the "shape" of the styled frame?? I tried to look for a QStyle method, but I wasn't able to get the real shape of the frame.

    Another solution could be:

    a) paint the filled rect
    b) paint the rounded frame

    The point is that (b) doesn't clear the area outside the frame, so (a) remains even there. This is the way I've implemented it now.

    Can you help me finding out a good solution for my need?

    Thanks a lot!

    P.S.: Surprisingly, it seems to work fine when built for Windows, but I cannot explain why.

  • Moderators

    QPainter class has some "clipping": capabilities.

    What type of QStyle has your widget? You could step into QStyle::drawControl() and check where the issue is.

Log in to reply