2

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?

László Papp
  • 51,870
  • 39
  • 111
  • 135
sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • 2
    Check out `QPaintEvent`, which you get as method parameter. – hyde Mar 07 '14 at 19:52
  • @hyde OMG you're right! `QPaintEvent::rect()` was just what I needed. Thanks, that answers my question, you can post it as an answer and I'll accept it. – sashoalm Mar 07 '14 at 21:52

1 Answers1

2

I would try this instead:

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

See the documentation for details.

László Papp
  • 51,870
  • 39
  • 111
  • 135