0

I want to draw a line on top of the widget, but visually QPainter draws it under the widget if the widget has background color.

Paint event function in which I'm trying to draw the line:

void PaintHelper::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);

    // Set up the "line"
    QRect rect = ui->frame->frameRect();
    rect.setY(ui->frame->parentWidget()->size().rheight() / 2);
    rect.setHeight(5);
    rect.setWidth(rect.width() * 1.5);

    // Draw the line
    QPainter painter(this);
    painter.setPen(QPen(QColor(234, 33, 123), 20));
    painter.drawRect(rect);
}

The button (see the screenshot below) is needed to call repaint (this didn't help anyway)

void PaintHelper::on_pushButton_clicked()
{
    repaint();
}

And the simplest .qss file for that QFrame on which I want to draw the line:

QFrame{
    background-color: #B0A686;
}

Thats how it looks

  • 1
    Seems you are painting to the parent widget (`PaintHelper`) and then the child frame (`ui->frame`) is painted over it. This is natural way how Qt works, parent widget is painted and then its children are painted over it. You will need to override the `paintEvent` method of the frame instead of the `paintEvent` method of the parent widget. But it will not be easy, you will have to define your custom class derived from `QFrame` and then use this derived class in UI form. Or you should be also able to do it using event filter but it will be somewhat more hackish... – HiFile.app - best file manager Sep 11 '22 at 12:50
  • Yeah, seems I have to create a child class. Thanks – Mikel Grenlou Sep 12 '22 at 07:06

0 Answers0