问题
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?
回答1:
I would try this instead:
...
setMinimumHeight(3000);
setMinimumWidth(3000);
}
void MyWidget::paintEvent(QPaintEvent *paintEvent)
{
qDebug() << paintEvent.rect();
...
See the documentation for details.
来源:https://stackoverflow.com/questions/22259173/get-visible-area-of-qpainter