Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How QPainter repaint viewport() and how to optimize it?
Qt 6.11 is out! See what's new in the release blog

How QPainter repaint viewport() and how to optimize it?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 696 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by
    #1

    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:

    1. What does paint do, when I call repaint()? Is it draw the whole widget or only what I see on the screen ( viewport )?
    2. 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 )?
    3. 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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Draw the stuff outside your paintEvent() and only blit the QImage/QPixmap in the paintEvent()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved