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 QScrollArea repaints QWidget on it?
Qt 6.11 is out! See what's new in the release blog

How QScrollArea repaints QWidget on it?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 606 Views 1 Watching
  • 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 one big widget ( inherits QWidget ) on it, like this:

        area = new QScrollArea(this);
        bigWidget = new bigWidgetClass;
        bigWidget->setFixedSize(5000,5000);
        area->setWidget(bigWidget);
    

    In bigWidget I draw numbers, like this:

    void bigWidgetClass::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        auto reg = visibleRegion();
    
        for(int i=0; i<400; i++)
        {
            for(int j=0;j<400;j++)
            {
                if(reg.contains(QPoint(j*30,i*30)))
                {
                    painter.drawText(j*30,i*30,QString::number((i)%10000));
                }
            }
        }
    }
    

    I check if point is visible - drawText. If not - don't drawText: if(reg.contains(QPoint(j*30,i*30)))
    I would like not to draw text, which I will don't see on screen, because I would like to have faster application.

    When I change QScrollArea's ScrollBar value using mouse I don't see all text pixels:

    prob.png

    When I minimize application, and next show it - everything is ok ( I see all text pixels ).

    So how QScrollArea repaint screen? Using repaint(QRegion) ?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #4

      I think you have to test that the visibleRegion and the rect occupied by your circle, text or whatever don't intercept:
      see: intersects(const QRect &rect) const

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #2

        I think you should test the text rectangle occupied by the number instead. see QFontMetrics.

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qwe3
          wrote on last edited by
          #3

          @mpergand The texts is only example. In my real application I have circles, rects etc. And I don't understand when the text is in the center of visible region - I don't see pixels too...

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #4

            I think you have to test that the visibleRegion and the rect occupied by your circle, text or whatever don't intercept:
            see: intersects(const QRect &rect) const

            1 Reply Last reply
            2
            • M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #5

              Quick dirty example:

              class BigWidget : public QWidget
              {
              
                  void paintEvent(QPaintEvent *event)
                  {
                     QPainter painter(this);
                     auto reg = visibleRegion();
                     int w=80;
                     QSize s{w,w};
                     QPoint pv=reg.boundingRect().topLeft();
                     QRect rv(pv,s);
                     
                     while(reg.intersects(rv))
                         {
                         painter.drawRect(rv);
                         painter.drawText(rv, Qt::AlignCenter,QString("%1,%2").arg(rv.x()).arg(rv.y()));
                       
                         QPoint ph=pv+QPoint(w,0);
                         QRect rh=QRect(ph,s);
                         
                         while(reg.intersects(rh))
                          {
                          qDebug()<<rh;
                          painter.drawRect(rh);
                          painter.drawText(rh, Qt::AlignCenter,QString("%1,%2").arg(rh.x()).arg(rh.y()));
                          ph+=QPoint(w,0);
                           rh=QRect(ph,s);  
                          }
                         
                         pv+=QPoint(0,w);
                         rv=QRect(pv,s);
                         }      
                  }
              };
              
              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