Get visible area of QPainter

半城伤御伤魂 提交于 2019-12-24 01:42:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!