Question

I have an owner-drawn QWidget inside a QScrollArea, so when painting, and I want to paint only the parts that are visible. To do so, I need to have the rectangle of the visible area of the QPainter.

The only candidates were QPainter::viewport(), QPainter::window(), and QPainter::clipBoundingRect(), so I put this code to log their output:

    setMinimumHeight(3000);
    setMinimumWidth(3000);
}
void MyWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    qDebug() << painter.viewport() << painter.window() << painter.clipBoundingRect();

Then I moved the horizontal and vertical scrollbars, but the logged output was strange:

QRect(0,0 3000x3000) QRect(0,0 3000x3000) QRectF(-21,-21 0x0) 
QRect(0,0 3000x3000) QRect(0,0 3000x3000) QRectF(-1,-21 0x0) 
QRect(0,0 3000x3000) QRect(0,0 3000x3000) QRectF(-1,-1 0x0) 

As, you can see, none of these functions give the actual visible area, how do I get it?

Was it helpful?

Solution

I would try this instead:

...
    setMinimumHeight(3000);
    setMinimumWidth(3000);
}
void MyWidget::paintEvent(QPaintEvent *paintEvent)
{
    qDebug() << paintEvent.rect();
...

See the documentation for details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top