Results 1 to 3 of 3

Thread: Using a QTimer to control a QProgressBar

  1. #1
    Join Date
    Aug 2010
    Location
    Joplin, Mo
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using a QTimer to control a QProgressBar

    The code that follows is from a trivial app I wrote in order to get familiar with Qt & reacquainted with C++. After getting some help from this forum with the Signals & Slots, I added a QProgressBar & a QTimer to control it (since I don't really have any process to monitor the progress of.) It works but it's kind of clunky. The progress bar doesn't progress smoothly. It updates in intervals of double the last value (i.e. 1,2,4,8,16, etc...) instead of each 1 second increment. I know there must be a better way to code this. Probably a much better way to implement what I'm trying to do. The pertinent portion of the code is pasted below.

    Constructive criticism with examples are always appreciated. I really do want to learn (relearn) how to do this properly. It's been several years since I tried to code anything at all.

    Thanx,

    Ed

    Qt Code:
    1. void SandSDemo::cancel2Text()
    2. {
    3. QString * cancel2Msg = new QString("Operation confirmed. Proceeding with deletion of all hard drive partitions.");
    4. ui->msgArea->setText(*cancel2Msg);
    5. delete cancel2Msg;
    6. ui->cancel2Button->hide();
    7. counter = 0; // START CODE SNIPPET HERE
    8. updateProgressBar();
    9. }
    10.  
    11. void SandSDemo::updateProgressBar()
    12. {
    13. timer = new QTimer(this);
    14. connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressBar()));
    15. timer->start(1000);
    16. if(counter <= 100)
    17. {
    18. counter++;
    19. ui->progressBar->setValue(counter);
    20. }
    21. else
    22. {
    23. timer->stop();
    24. delete timer; //END CODE SNIPPET HERE
    25. QString * finalMsg = new QString("Operation completed. All hard drive partitions have been deleted.");
    26. ui->msgArea->setText(*finalMsg);
    27. delete finalMsg;
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Using a QTimer to control a QProgressBar

    You call updateProgressBar every second, then a new timer is created, so 2 timers call your function, then 2 more timers are created (and make a connect), then your function is called 4 times etc. etc. etc. etc. So, you have to create the timer outside updateProgressBar()!
    Last edited by Lykurg; 26th August 2010 at 22:59. Reason: spelling corrections

  3. #3
    Join Date
    Aug 2010
    Location
    Joplin, Mo
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using a QTimer to control a QProgressBar

    Thank you. That was so obvious AFTER you pointed it out.

    Ed

Similar Threads

  1. Using QProgressBar
    By gutiory in forum Qt Programming
    Replies: 6
    Last Post: 5th May 2010, 06:59
  2. Replies: 0
    Last Post: 16th December 2009, 09:45
  3. QProgressBar in a new window
    By tommy in forum Qt Programming
    Replies: 2
    Last Post: 13th March 2009, 17:24
  4. QProgressBar + Mac OS X
    By THRESHE in forum Qt Programming
    Replies: 5
    Last Post: 14th December 2007, 13:41
  5. QProgressBar & 200%
    By Dmitry in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2006, 11:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.