How QPainter repaint viewport() and how to optimize it?
-
Hi,
I have QScrollArea and big widget on it, like this:
MainWindow:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); myBigWidgetObject = new myBigWidget(this); scrollArea = new QScrollArea(this); scrollArea->resize(500,500); scrollArea->setWidget(myBigWidgetObject); }
big widget:
myBigWidget::myBigWidget(QWidget *parent): QWidget(parent) { setFixedSize(5000,5000); } void myBigWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); for(int i=0; i<100; i++) { painter.drawRect(i*100,i*100,50,50); } for(int i=0; i<100; i++) { painter.drawEllipse(5000-i*100,i*100,50,50); } ... }
In my real app I have bigger widget and more expensive operations in paintEvent() than in this example above.
I have QTimer, which very often scrolling myBigWidgetObject and do repaint() on this object ( I have many if's statements in paintEvent() ).
And my questions are:
- What does paint do, when I call repaint()? Is it draw the whole widget or only what I see on the screen ( viewport )?
- What does paint do, when I call change value on scrollBar in QScrollArea? Is it draw the whole widget or only what I see on the screen ( viewport )?
- What is more expensive: call something like
painter->drawRect(.....)
or really draw this rect on the screen?
I would like to optimize this drawing because I draw this widget many times.
I see something like QRegion. Maybe this is a good idea?
-
Draw the stuff outside your paintEvent() and only blit the QImage/QPixmap in the paintEvent()