Painting in same widget above child objects of Itself

  • Qt Champions 2017

    Sample code below.

    1. I have widget with child buttons.
    2. paintEvent(..) reimplemented. I'm drawing the line in paintEvent.

    Issue - Line always below the buttons. I would like to see the line above the buttons. Is there a way to achieve this ?

    MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)

    void MyWidget::createUI()
    b1 = new QPushButton("Monitor");
    b2 = new QPushButton("Help");

    hLyt = new QHBoxLayout;


    void MyWidget::paintEvent(QPaintEvent *event)
    QPainter paint(this);
    QPen pen;

  • Lifetime Qt Champion


    IIRC, in that kind of situation, you would rather have an overlay widget on top of the other and paint that one.

  • Moderators

    @dheerendra The children are always drawn on top of the widget. So I don't think there's a way to do that other than putting another widget on top like @SGaist suggested.

    I helped someone on the forums here recently with a tour my app type widget that did something similar. And I had to make it part of the widget I wanted to highlight. I couldn't just draw over other widgets. There may be a way but nothing popped into my mind while I was doing that.

  • Qt Champions 2017

    Thank you @SGaist @ambershark. I have achieved my requirements through overlay. Just wanted to check possibility. I think it does not make sense to draw parent over children. So my question itself is not valid :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.