PDA

View Full Version : thread->start() does not work ?



ErrMania
20th November 2011, 01:34
Hello,

i'm doing the same thing for an hour, i followed this tutorial on Youtube (http://www.youtube.com/watch?v=PR6wVy7Et1A&feature=BFa&list=ULUX6DVOUujWQ&lf=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;
}

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



dialog.cpp :

#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));
}


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


mythread.cpp :

#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);
}
}


Thanks for your help !

NullPointer
20th November 2011, 04:26
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

Santosh Reddy
20th November 2011, 05:13
Quickly there are problems with the example link you mentioned. Some quick observations

1. MyThread::stop is not initialized.

MyThread::MyThread(QObject *parent) :
QThread(parent)
{
Stop = false;
}


2. QMutex is of no use there, it should not be used that way. Refer Documentation

[ (http://doc.qt.nokia.com/4.7-snapshot/qmutex.html#details)Edit]
Also refer the comments below the video it will solve your problems.

ErrMania
20th November 2011, 11:08
: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 !