Hello,

I have simple gui application. This application creates new background thread. There are some operations to execute in the thread with progress reporting to gui thread.

Some code:
backthread.h:
Qt Code:
  1. #ifndef BACKTHREAD_H
  2. #define BACKTHREAD_H
  3.  
  4. #include <QThread>
  5. #include "Counter.h"
  6.  
  7.  
  8. class BackThread : public QThread
  9. {
  10. public:
  11. virtual void run();
  12.  
  13. private:
  14. Counter counter;
  15.  
  16.  
  17. };
  18.  
  19. #endif // BACKTHREAD_H
To copy to clipboard, switch view to plain text mode 

backthread.cpp:
Qt Code:
  1. #include "backthread.h"
  2.  
  3. void BackThread::run()
  4. {
  5. int i=0;
  6. while(1)
  7. {
  8. counter.setValue(i);
  9. sleep( 1 );
  10. qDebug( "Ping!" );
  11. i+=10;
  12. if(i==100) i=0;
  13. }
  14. }
To copy to clipboard, switch view to plain text mode 

Counter.h:
Qt Code:
  1. #ifndef COUNTER_H
  2. #define COUNTER_H
  3.  
  4.  
  5. #include <QObject>
  6.  
  7. class Counter : public QObject
  8. {
  9. Q_OBJECT
  10.  
  11. public:
  12. Counter() { m_value = 0; }
  13.  
  14. int value() const { return m_value; }
  15.  
  16. public slots:
  17. void setValue(int value);
  18.  
  19. signals:
  20. void valueChanged(int newValue);
  21.  
  22. private:
  23. int m_value;
  24. };
  25.  
  26. #endif
To copy to clipboard, switch view to plain text mode 

Counter.cpp:
Qt Code:
  1. #include "Counter.h"
  2.  
  3. void Counter::setValue(int value)
  4. {
  5. if (value != m_value) {
  6. m_value = value;
  7. emit valueChanged(value);
  8. }
  9. }
To copy to clipboard, switch view to plain text mode 

And in main windows class:
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent)
  5. : QMainWindow(parent), ui(new Ui::MainWindow)
  6. {
  7. ui->setupUi(this);
  8.  
  9. BackThread th;
  10. th.start();
  11. }
  12.  
  13. MainWindow::~MainWindow()
  14. {
  15. delete ui;
  16. }
To copy to clipboard, switch view to plain text mode 

There is Progress Bar widget in main window, and its value should be updated when Counter::m_progress changes. But I have no idea how to define slot for main window class to receive notifications from thread.

Can you help me?