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