Painting in same widget above child objects of Itself



  • 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)
    {
    createUI();
    }

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

    hLyt = new QHBoxLayout;
    hLyt->addWidget(b1);
    hLyt->addWidget(b2);
    
    this->setLayout(hLyt);
    

    }

    void MyWidget::paintEvent(QPaintEvent *event)
    {
    QPainter paint(this);
    paint.setBrush(Qt::blue);
    QPen pen;
    pen.setWidth(4);
    pen.setBrush(Qt::red);
    paint.setPen(pen);
    paint.drawLine(30,0,30,this->rect().height());
    }
    0_1495032721254_Scree_Line_Below.png


  • Lifetime Qt Champion

    Hi,

    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.



  • 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.