thread->start() does not work ?
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 :
Code:
#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
dialog.cpp :
Code:
#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
));
}
mythread.h :
Code:
#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
mythread.cpp :
Code:
#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);
}
}
Thanks for your help !
Re: thread->start() does not work ?
Hello,
I don't think that this is the source of yout problem, but the void run() is a protected method of QThread...
Someone will tell you that using the break is not a good idea...
Added after 11 minutes:
Now, testing your code:
Mutex implementation is not so Ok. Instead of it, made the mutex be a member of the thread... Maybe you don't need it, since it is more interesting to protect on writing then reading.
But the biggest problem is with the Stop. Not initializing it, give it a random value, which is breaking the for.
Try a Stop=false; in class constructor ;)
HTH
Re: thread->start() does not work ?
Quickly there are problems with the example link you mentioned. Some quick observations
1. MyThread::stop is not initialized.
Code:
MyThread
::MyThread(QObject *parent
) :{
Stop = false;
}
2. QMutex is of no use there, it should not be used that way. Refer Documentation
[Edit]
Also refer the comments below the video it will solve your problems.
Re: thread->start() does not work ?
:eek: ah I was close! i thought stop was declared to false as it is a boolean, but no :D Thanks a lot for your help !