Results 1 to 6 of 6

Thread: Using a QProgressbar in a For Loop

  1. #1
    Join Date
    Oct 2012
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Using a QProgressbar in a For Loop

    Hi! I want to use a Qprogressbar using a For Loop. Every time the value inside the for loop changes, it sets the value of the progress bar. A sample code will be helpful.


    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using a QProgressbar in a For Loop

    Qt Code:
    1. for(int i=0;i<100;++i) { progressBar->setValue(i); qApp->processEvents(); }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2012
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using a QProgressbar in a For Loop

    thanks wysota. regarding process events, i tried it now, but it seems the process becomes slow. is there a way to adjust the process time? the process im doing is transferring of files.
    Thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using a QProgressbar in a For Loop

    Quote Originally Posted by kiboi View Post
    thanks wysota. regarding process events, i tried it now, but it seems the process becomes slow.
    Obviously as your application has much more to do.

    is there a way to adjust the process time? the process im doing is transferring of files.
    e.g.
    Qt Code:
    1. for(int i=0;i<100;++i) { progressBar->setValue(i); if(i % 100 == 0) qApp->processEvents(); }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2012
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using a QProgressbar in a For Loop

    my concern is that i want to prevent the application to get into "not responding" status if seen at the task manager while doing the for loop. i'll try your suggestion.thanks!

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using a QProgressbar in a For Loop

    You can always copy files in a secondary thread, e.g. something like this might work:

    Qt Code:
    1. struct FileStruct {
    2. QString sourcePath;
    3. QString destPath;
    4. FileStruct(s, d) : sourcePath(s), destPath(d) {}
    5. };
    6.  
    7. bool copyFile(FileStruct s) {
    8. QFile::copy(s.sourcePath, s.destPath);
    9. return true;
    10. }
    11.  
    12. QList<FileStruct> filesToCopy;
    13. filesToCopy << FileStruct(...,...) << FileStruct(...,...) << FileStruct(...,...);
    14.  
    15. QFuture<bool> result = QtConcurrent::mapped(filesToCopy, copyFile);
    To copy to clipboard, switch view to plain text mode 

    but it will probably be slower than copying one file at a time (unless you have a SSD disk).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to use a QProgressbar
    By Niamita in forum Qt Programming
    Replies: 5
    Last Post: 16th March 2012, 12:05
  2. Replies: 4
    Last Post: 6th August 2011, 01:40
  3. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.