Results 1 to 3 of 3

Thread: set progress bar values inside a thread

  1. #1
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default set progress bar values inside a thread

    Hi all!

    I wrote a progress bar class that has a Gui part and a thread part. In the way I have now the thread has a "for" loop that emits a signal to the Gui class and then has a 100ms sleep.

    This works but the gui kinda frezzes so I was trying to implement it in another way.
    I want to send the progress bar object to the thread class and set the value from there. When I tried this the programm crashed and I need some help.

    here the code:

    Qt Code:
    1. #include "progressbar.h"
    2.  
    3. class SleepThread : public QThread
    4. {
    5. public:
    6. static void mySleep(int nMSecs)
    7. {
    8. msleep(nMSecs);
    9. }
    10. };
    11.  
    12.  
    13. progressBar::progressBar(QWidget *parent)
    14. : QDialog(parent)
    15. {
    16. ui.setupUi(this);
    17. }
    18.  
    19. progressBar::~progressBar()
    20. {
    21.  
    22. }
    23.  
    24. void progressBar::changeMessage(QString text){ //this is where I update the message from my main gui
    25. ui.label->setText(text);
    26. }
    27.  
    28. void progressBar::init(){ //this is to open and show the progress bar
    29. this->show();
    30. pbarThread = new barThread;
    31. pbarThread->setProgressBar(ui.progressBar); //set the progress bar pointer
    32. connect(pbarThread, SIGNAL(setValue(int)), this, SLOT(setPBarValue(int)));
    33. pbarThread->start();
    34. }
    35.  
    36. void progressBar::setPBarValue(int i){ //this is how I set the value but I don't want to use this function anymore
    37. ui.progressBar->setValue(i);
    38. if(i == 100){
    39. if(ui.progressBar->invertedAppearance()){
    40. ui.progressBar->setInvertedAppearance(false);
    41. }else{
    42. ui.progressBar->setInvertedAppearance(true);
    43. }
    44. }
    45. }
    46.  
    47. void progressBar::stopPBar(){ //to stop the thread before I delete the object
    48. this->hide();
    49. pbarThread->stopThread();
    50. while(!pbarThread->isFinished()){
    51. //Wait on loop until thread is finished
    52. }
    53. }
    54.  
    55. void barThread::stopThread(){
    56. running = false;
    57. }
    58.  
    59. void barThread::run(){
    60.  
    61. running = true;
    62.  
    63. while(running){
    64. for(int i = 0; i < 101; i=i+5){
    65. _progressBar->setValue(i); //This is where it crashes now
    66. // emit setValue(i); //the signal I emited before to alert the Gui class
    67. if(!running) break;
    68. SleepThread::mySleep(100);
    69. }
    70. }
    71. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #ifndef PROGRESSBAR_H
    2. #define PROGRESSBAR_H
    3.  
    4. #include <QtGui>
    5. #include "ui_progressbar.h"
    6. #include <QtCore>
    7. #include <qthread.h>
    8.  
    9. class barThread : public QThread
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. bool running;
    15. void stopThread();
    16. void setProgressBar(QProgressBar *pb){_progressBar = pb;}
    17.  
    18. protected:
    19. void run();
    20.  
    21. private:
    22. QProgressBar *_progressBar;
    23.  
    24. signals:
    25. void setValue(int);
    26. };
    27.  
    28. class progressBar : public QDialog
    29. {
    30. Q_OBJECT
    31.  
    32. public:
    33. progressBar(QWidget *parent = 0);
    34. ~progressBar();
    35.  
    36. void init();
    37. void stopPBar();
    38. void changeMessage(QString);
    39.  
    40. private slots:
    41. void setPBarValue(int);
    42.  
    43. private:
    44. Ui::progressBarClass ui;
    45.  
    46. barThread *pbarThread;
    47. };
    48.  
    49. #endif // PROGRESSBAR_H
    To copy to clipboard, switch view to plain text mode 

    I already did once a class that showed a live picture from a thread and the method was the same.

    Thanks in advance

  2. #2
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: set progress bar values inside a thread

    1) Why do you have mySleep in a separate class? It just confuses things?

    2) Why are you using sleep rather than a repetitive timer? (Then you wouldn't even need a separate thread.)

    3) You should probably use Qt::QueuedConnection for the 5th parm of connect(). It should be defaulting to that, but it doesn't hurt to be specific.

  3. #3
    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: set progress bar values inside a thread

    All GUI stuff has to be in the main thread. Also accessing GUI parts from a custom thread is a very bad idea. So your signal/slot connection is the right thing to do. If it was freezing, then you probably called the thread function in a wrong way.

Similar Threads

  1. Progress Bar set value inside OpenMP parallel for
    By lixo1 in forum Qt Programming
    Replies: 2
    Last Post: 18th February 2010, 19:51
  2. Thread updates progress bar
    By GianMarco in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2009, 13:29
  3. busy progress bar without thread ?
    By npc in forum Newbie
    Replies: 34
    Last Post: 23rd July 2009, 09:29
  4. Display progress on another thread
    By radu_d in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2007, 08:02
  5. Progress Bar with double values
    By ^NyAw^ in forum Qt Programming
    Replies: 8
    Last Post: 23rd March 2007, 15:37

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.