Results 1 to 7 of 7

Thread: How to use QProgressBar correctly

  1. #1
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to use QProgressBar correctly

    I am at a lose here on how to achieve what I am trying to do in my current application. Basically what happens is that there is a custom QTreeView that when the user drags and drops and item onto another item, the underlying data is changed and it can take several minutes for the application to move all the data correctly. I am trying to use a QProgressBar to show the user that the data is being moved and that the program is still working just needs some time to process the information.

    Here is the code that occurs when a dropEvent happens:

    Qt Code:
    1. void ClassA::dropEvent(QDropEvent *event)
    2. {
    3. // doing some stuff
    4. emit viewChanged()
    5. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow class handles the emitted changed like so...

    Qt Code:
    1. connect(classA, SIGNAL(viewChanged()), this, SLOT(updateViewChanged()));
    To copy to clipboard, switch view to plain text mode 

    The corresponding slot makes some data changes and then a private function inside of this slot does a lot of heavy lifting.

    Qt Code:
    1. void MainWindow::updateViewChanged()
    2. {
    3. // doing some stuff
    4. // now making call to heavy lifting function
    5. updateData()
    6. }
    To copy to clipboard, switch view to plain text mode 

    The updateData() function can take several minutes to update information and what it is doing is changing values in QHash tables and some QLists in order for the information to be written back to the corresponding files correctly. Inside this function there are several loops that are time consuming.

    Qt Code:
    1. void MainWindow::updateData()
    2. {
    3. QProgress progress(this);
    4. // set min/max appropriately with setMinimum(int) and setMaximum(int)
    5. // set variable called counter to increment the value of progressBar
    6.  
    7. // nested loops
    8. while()
    9. {
    10. progress.setValue(counter++);
    11.  
    12. for()
    13. {
    14. progress.setValue(counter++);
    15. for()
    16. {
    17. progress.setValue(counter++);
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    Basically when this function is called everything happens as should but no progress bar is displayed and I have no clue why. If I add qApp->processEvents() under each progress.setValue() call then the operation takes forever and still no progress bar displayed.

    Any suggestions or help would be appreciated because I am not sure how to accomplish what I want to happen. FYI, I know this would make QProgressDialog every time the function is called that won't be the end result but I am just trying to get the darn bar to show!

    Thanks for any help!!

  2. #2
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use QProgressBar correctly

    progress.setRange(0,100); ?

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to use QProgressBar correctly

    Are you using a QProgressBar or a QProgressDialog? The former needs to be created, placed into a layout and shown before it is visible; the latter provides all that infrastructure for you by placing the progress bar into a dialog.

    You probably won't need to call processEvents, but if you do you don't want to call it any more often than is necessary to ensure timely progress bar updates. It's impossible to tell from the code sketch you provided where this should be - it could be just inside the while loop, just inside the outermost for loop, in the innermost for loop, or perhaps when the value of counter modulus some number equals zero. But you certainly don't want to call it every time counter gets incremented.

  4. The following user says thank you to SixDegrees for this useful post:

    jshafferman (15th April 2011)

  5. #4
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use QProgressBar correctly

    Thanks SixDegress! I didn't know that you had to place the QProgressBar into the layout before it was visible, this would be the reason I am not seeing it when the application starts the heavy lifting process.

    I don't think I need to use processEvents at all but if I wanted to I think if I used it on the outer while loop I would get the intended response. Thank you very much for clearing that up for me! I have used QProgressDialog before so I had assumed they worked the same only one was a dialog with options while the other had no options just the progress bar.

    Quick question however, if this is happening in MainWindow (which is sublcass of QMainWindow) do I just add it to the main layout. The MainWindow's central widget is a QSplitter so I am not sure how to do this because I want the progress bar to be centered in the main widget. Thanks for any additional help.

  6. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to use QProgressBar correctly

    If you want your progress bar centered on top of the main window, I'd suggest using the QProgressDialog again. Giving it the main window as parent will start it off centered over that widget, and if you want the window blocked you can set it to be modal, or non-modal otherwise.

  7. #6
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use QProgressBar correctly

    Thanks I will go ahead and use the QProgressDialog again, is there anyway to hide the buttons on the QProgressDialog? I know I can simply not react to any signal being sent by a click but I am curious if there is a 'hide' option. Thanks again for your help!

  8. #7
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use QProgressBar correctly

    I figured out how to remove the cancel button, setCancelButton(0) removes the Cancel button

Similar Threads

  1. setBar ( QProgressBar * bar )
    By hvw59601 in forum Qt Programming
    Replies: 7
    Last Post: 29th July 2010, 00:09
  2. Using QProgressBar
    By gutiory in forum Qt Programming
    Replies: 6
    Last Post: 5th May 2010, 06:59
  3. QProgressBar + Mac OS X
    By THRESHE in forum Qt Programming
    Replies: 5
    Last Post: 14th December 2007, 13:41
  4. QProgressBar confusion
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 23rd November 2006, 09:20
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.