Hello,

i'm doing the same thing for an hour, i followed this tutorial on Youtube (http://www.youtube.com/watch?v=PR6wV...f=mfu_in_order), about threads, and i don't understand why my code doesn't work :

my label is supposed to take a number from 0 to 1000, but the label's content doesn't change, the text in the label stays "as is", if i put the text "number" in it, this string "number" won't be changed by an int from 0 to 1000. Here's my code :

dialog.h :
Qt Code:
  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3.  
  4. #include <QDialog>
  5. #include <QtGui>
  6. #include "mythread.h"
  7.  
  8. namespace Ui {
  9. class Dialog;
  10. }
  11.  
  12. class Dialog : public QDialog
  13. {
  14. Q_OBJECT
  15.  
  16. public:
  17. explicit Dialog(QWidget *parent = 0);
  18. ~Dialog();
  19. MyThread *mThread;
  20.  
  21. public slots:
  22. void onNumberEmit(int);
  23.  
  24. private slots:
  25. void on_pushButton_clicked();
  26. void on_pushButton_2_clicked();
  27.  
  28. private:
  29. Ui::Dialog *ui;
  30. };
  31.  
  32. #endif // DIALOG_H
To copy to clipboard, switch view to plain text mode 


dialog.cpp :
Qt Code:
  1. #include "dialog.h"
  2. #include "ui_dialog.h"
  3.  
  4. #include "mythread.h"
  5.  
  6. Dialog::Dialog(QWidget *parent) :
  7. QDialog(parent),
  8. ui(new Ui::Dialog)
  9. {
  10. ui->setupUi(this);
  11.  
  12. mThread = new MyThread(this);
  13.  
  14. connect(mThread, SIGNAL(NumberEmit(int)), this, SLOT(onNumberEmit(int)));
  15. }
  16.  
  17. Dialog::~Dialog()
  18. {
  19. delete ui;
  20. }
  21.  
  22. void Dialog::on_pushButton_clicked()
  23. {
  24. //QMessageBox::warning(this, "alert", "all");
  25. mThread->start();
  26. }
  27.  
  28. void Dialog::on_pushButton_2_clicked()
  29. {
  30. mThread->Stop = true;
  31.  
  32. }
  33.  
  34. void Dialog::onNumberEmit(int num){
  35. ui->label->setText(QString::number(num));
  36. }
To copy to clipboard, switch view to plain text mode 

mythread.h :
Qt Code:
  1. #ifndef MYTHREAD_H
  2. #define MYTHREAD_H
  3.  
  4. #include <QThread>
  5.  
  6. class MyThread : public QThread
  7. {
  8. Q_OBJECT
  9. public:
  10. explicit MyThread(QObject *parent = 0);
  11. void run();
  12. bool Stop;
  13.  
  14. signals:
  15. void NumberEmit(int);
  16.  
  17. public slots:
  18.  
  19. };
  20.  
  21. #endif // MYTHREAD_H
To copy to clipboard, switch view to plain text mode 

mythread.cpp :
Qt Code:
  1. #include "mythread.h"
  2. #include <QtCore>
  3.  
  4. MyThread::MyThread(QObject *parent) :
  5. QThread(parent)
  6. {
  7. }
  8.  
  9. void MyThread::run(){
  10. for (int i=0; i<1000; i++){
  11. QMutex mutex;
  12. mutex.lock();
  13. if (this->Stop) break;
  14. mutex.unlock();
  15.  
  16. //this->msleep(100);
  17.  
  18. emit NumberEmit(i);
  19. }
  20. }
To copy to clipboard, switch view to plain text mode 

Thanks for your help !