QPainter paints under the widget's background color (background defined in styleSheet)

519 Views Asked by At

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

0

There are 0 best solutions below