Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8584931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:53:29+00:00 2026-06-11T21:53:29+00:00

Basically what I wanna do is to draw rectangles for each number in my

  • 0

Basically what I wanna do is to draw rectangles for each number in my list. The bigger the number is, the larger the rectangle is.
My problem is when I actually wanna do it, step-by-step, and waiting a few seconds between every drawing. I’ve looked out for a few solutions but I can’t get them to work for this particular case. I saw I could use fflush to release whatever it’s in the buffer but I don’t know how I can use it for this.

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
int weight=300/lista.size;
int posx=weight;
for (int i=1; i<=lista.size; i++){
        List_node * node = list.get_element_at(i);
            int num=node->getValue(); //this returns the value of the node
        if (i==3){
                painter.setBrush(QBrush(Qt::red, Qt::SolidPattern)); // this line is to draw a rectangle with a different color. Testing purposes.
         }
        painter.drawRect(posx,400-(num*10),weight,num*10);
        sleep(1); //this sleep isn't working correctly.
        painter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
        posx+=weight;
 }

Any help would be really appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:53:30+00:00Added an answer on June 11, 2026 at 9:53 pm

    sleep() won’t work for this — it blocks the Qt event loop and keeps Qt from doing its job while it is sleeping.

    What you need to do is keep one or more member variables to remember the current state of the image you want to draw, and implement paintEvent() to draw that current single image only. paintEvent() (like every function running in Qt’s GUI thread) should always return immediately, and never sleep or block.

    Then, to implement the animation part of things, set up a QTimer object to call a slot for you at regular intervals (e.g. once every 1000mS, or however often you like). Implement that slot to adjust your member variables to their next state in the animation-sequence (e.g. rectangle_size++ or whatever) and then call update() on your widget. update() will tell Qt to call paintEvent() again on your widget as soon as possible, so your display will be updated to the next frame very shortly after your slot method returns.

    Below is a trivial example of the technique; when run it shows a red rectangle getting larger and smaller:

    // begin demo.h
    #include <QWidget>
    #include <QTimer>
    
    class DemoObj : public QWidget
    {
    Q_OBJECT
    
    public:
       DemoObj();
    
       virtual void paintEvent(QPaintEvent * e);
    
    public slots:
       void AdvanceState();
    
    private:
       QTimer _timer;
       int _rectSize;
       int _growthDirection;
    };
    
    // begin demo.cpp
    #include <QApplication>
    #include <QPainter>
    #include "demo.h"
    
    DemoObj :: DemoObj() : _rectSize(10), _growthDirection(1)
    {
       connect(&_timer, SIGNAL(timeout()), this, SLOT(AdvanceState()));
       _timer.start(100);   // 100 milliseconds delay per frame.  You might want to put 2000 here instead
    }
    
    void DemoObj :: paintEvent(QPaintEvent * e)
    {
       QPainter p(this);
       p.fillRect(rect(), Qt::white);
       QRect r((width()/2)-_rectSize, (height()/2)-_rectSize, (_rectSize*2), (_rectSize*2));
       p.fillRect(r, Qt::red);
    }
    
    void DemoObj :: AdvanceState()
    {
       _rectSize += _growthDirection;
       if (_rectSize > 50) _growthDirection = -1;
       if (_rectSize < 10) _growthDirection = 1;
       update();
    }
    
    int main(int argc, char ** argv)
    {
       QApplication app(argc, argv);
    
       DemoObj obj;
       obj.resize(150, 150);
       obj.show();
       return app.exec();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically i wanna show total list items for each list item that contains sub
array moves contains just a couple of move I wanna draw on canvas. basically
I wanna develop an addon (basically a hack) for IPhone native phone app. My
In my Asp.Net project I wanna use Property Auto-wiring, e.g. for my ILogger. Basically
Basically what I am doing is giving a user a list of terms via
Basically I have converted a tab delimited txt file into a list containing a
Basically I wanna know if all the types in a particular namespace implements a
This is basically what I am trying to do. I wanna take a File
I basically wanna do what I'd call nested queries (but not in the nested
Basically all i wanna do is something like this. var myVar = $.load(phpscript.php,var=one); phpscript.php

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.