Hi,

I am trying to update a progressbar in a main widget from a job run from a qthread via a custom event.

The objective is to ensure the progress bar is updated as the "long job" is running. At the moment if I just run job from the main class the gui just freezes until the job is complete. I think threading it is the best of option for "real time" update of the qprogressbar. See my code below of a simple example of what I am trying to do. I do not think the thread is emitting the event correctly (i just get cout of thread and not event !!), I think the problem may be with the qobject receiver in the postevet, qApp.

Any suggestions or help would be great, its very frustating as I feel I am close to a solution.

main.cpp

Qt Code:
  1. /*
  2. * external loop main.cpp
  3. */
  4. #include "externalloop.h"
  5. #include <qapplication.h>
  6.  
  7. int main( int argc, char **argv )
  8. {
  9. QApplication a( argc, argv );
  10.  
  11. ExternalLoop externalloop;
  12. externalloop.resize( 200, 100 );
  13. externalloop.setCaption( "ExternalLoop" );
  14. a.setMainWidget( &externalloop );
  15. externalloop.show();
  16.  
  17. return a.exec();
  18.  
  19. }
To copy to clipboard, switch view to plain text mode 

extarnalloop.h

Qt Code:
  1. /*
  2. * externalloop.h
  3. */
  4. #ifndef GREY_BUTTON_H
  5. #define GREY_BUTTON_H
  6.  
  7. #include <qwidget.h>
  8. #include <qpushbutton.h>
  9. #include <qprogressbar.h>
  10. #include <qapplication.h>
  11. #include <qlayout.h>
  12. #include <qevent.h>
  13. #include <qthread.h>
  14. #include <iostream>
  15.  
  16.  
  17. class ExternalLoop : public QWidget
  18.  
  19. {
  20. Q_OBJECT
  21.  
  22. public:
  23. ExternalLoop( QWidget *parent = 0, const char *name = 0 );
  24. QProgressBar *myProgress;
  25. void customEvent(QCustomEvent * e);
  26.  
  27.  
  28. private:
  29.  
  30. public slots:
  31. void theLoop();
  32.  
  33. };
  34.  
  35. class myEvent : public QCustomEvent
  36. {
  37.  
  38. public:
  39.  
  40. myEvent(int i): QCustomEvent( QEvent::User ),v(i){};
  41. int done() const { return v; };
  42.  
  43. private:
  44. int v;
  45.  
  46.  
  47. };
  48.  
  49. class MyThread : public QThread {
  50.  
  51. public:
  52.  
  53. virtual void run();
  54.  
  55. };
  56.  
  57. #endif
To copy to clipboard, switch view to plain text mode 

externalloop.cpp

Qt Code:
  1. /*
  2. * externalloop.cpp
  3. */
  4. #include "externalloop.h"
  5.  
  6. using namespace std;
  7.  
  8. ExternalLoop::ExternalLoop( QWidget *parent, const char *name )
  9. : QWidget( parent, name )
  10. {
  11.  
  12.  
  13. QGridLayout *grid1 = new QGridLayout(this,2,1,10);
  14.  
  15. pb1 = new QPushButton("Loop", this);
  16. grid1->addWidget( pb1, 0, 0 );
  17. connect(pb1, SIGNAL( clicked() ), this, SLOT( theLoop() ) );
  18.  
  19. myProgress = new QProgressBar(100, this);
  20. grid1->addWidget( myProgress, 1, 0);
  21.  
  22. myProgress->setTotalSteps(10);
  23.  
  24.  
  25. }
  26. void ExternalLoop::theLoop()
  27. {
  28.  
  29. MyThread a;
  30. a.start();
  31. a.wait();
  32.  
  33.  
  34. }
  35.  
  36. void ExternalLoop::customEvent(QCustomEvent * e)
  37. {
  38.  
  39. myEvent* lEvent = (myEvent*)e;
  40. int var = lEvent->done();
  41. myProgress->setProgress(var);
  42. qApp->processEvents();
  43. cout << "event " << var << endl;
  44.  
  45. }
  46.  
  47. void MyThread::run()
  48. {
  49.  
  50. for( int count = 0; count < 11; count++ ) {
  51. sleep( 1 );
  52. cout << "thread " << count << endl;
  53. myEvent* lEvent = new myEvent(count);
  54. QApplication::postEvent( qApp, lEvent);
  55.  
  56. }
  57. }
To copy to clipboard, switch view to plain text mode