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 :
	
	#ifndef DIALOG_H
#define DIALOG_H
 
#include <QDialog>
#include <QtGui>
#include "mythread.h"
 
namespace Ui {
    class Dialog;
}
 
{
    Q_OBJECT
 
public:
    explicit Dialog
(QWidget *parent 
= 0);
     ~Dialog();
    MyThread *mThread;
 
public slots:
    void onNumberEmit(int);
 
private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
 
private:
    Ui::Dialog *ui;
};
 
#endif // DIALOG_H
        #ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QtGui>
#include "mythread.h"
namespace Ui {
    class Dialog;
}
class Dialog : public QDialog
{
    Q_OBJECT
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    MyThread *mThread;
public slots:
    void onNumberEmit(int);
private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
private:
    Ui::Dialog *ui;
};
#endif // DIALOG_H
To copy to clipboard, switch view to plain text mode 
  
dialog.cpp :
	
	#include "dialog.h"
#include "ui_dialog.h"
 
#include "mythread.h"
 
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
 
    mThread = new MyThread(this);
 
    connect(mThread, SIGNAL(NumberEmit(int)), this, SLOT(onNumberEmit(int)));
}
 
Dialog::~Dialog()
{
    delete ui;
}
 
void Dialog::on_pushButton_clicked()
{
    //QMessageBox::warning(this, "alert", "all");
    mThread->start();
}
 
void Dialog::on_pushButton_2_clicked()
{
    mThread->Stop = true;
 
}
 
void Dialog::onNumberEmit(int num){
    ui
->label
->setText
(QString::number(num
));
}
        #include "dialog.h"
#include "ui_dialog.h"
#include "mythread.h"
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    mThread = new MyThread(this);
    connect(mThread, SIGNAL(NumberEmit(int)), this, SLOT(onNumberEmit(int)));
}
Dialog::~Dialog()
{
    delete ui;
}
void Dialog::on_pushButton_clicked()
{
    //QMessageBox::warning(this, "alert", "all");
    mThread->start();
}
void Dialog::on_pushButton_2_clicked()
{
    mThread->Stop = true;
}
void Dialog::onNumberEmit(int num){
    ui->label->setText(QString::number(num));
}
To copy to clipboard, switch view to plain text mode 
  
mythread.h :
	
	#ifndef MYTHREAD_H
#define MYTHREAD_H
 
#include <QThread>
 
{
    Q_OBJECT
public:
    explicit MyThread
(QObject *parent 
= 0);
     void run();
    bool Stop;
 
signals:
    void NumberEmit(int);
 
public slots:
 
};
 
#endif // MYTHREAD_H
        #ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
class MyThread : public QThread
{
    Q_OBJECT
public:
    explicit MyThread(QObject *parent = 0);
    void run();
    bool Stop;
signals:
    void NumberEmit(int);
public slots:
};
#endif // MYTHREAD_H
To copy to clipboard, switch view to plain text mode 
  
mythread.cpp :
	
	#include "mythread.h"
#include <QtCore>
 
MyThread
::MyThread(QObject *parent
) :{
}
 
void MyThread::run(){
    for (int i=0; i<1000; i++){
        mutex.lock();
        if (this->Stop) break;
        mutex.unlock();
 
        //this->msleep(100);
 
        emit NumberEmit(i);
    }
}
        #include "mythread.h"
#include <QtCore>
MyThread::MyThread(QObject *parent) :
    QThread(parent)
{
}
void MyThread::run(){
    for (int i=0; i<1000; i++){
        QMutex mutex;
        mutex.lock();
        if (this->Stop) break;
        mutex.unlock();
        //this->msleep(100);
        emit NumberEmit(i);
    }
}
To copy to clipboard, switch view to plain text mode 
  
Thanks for your help !
				
			
Bookmarks