PDA

View Full Version : why doesn't signal gets emiited?



naturalpsychic
26th January 2011, 15:29
i dont understand why are my signals not working in this multi threaded app

file.h:


#include <QObject>


class FThread;

class File : public QObject
{
Q_OBJECT
public:
File(QObject *parent = 0);
FThread *fthread;
public slots:
void print();



};

fthread.h


#include <QThread>


class File;
class FThread : public QThread
{
Q_OBJECT
public:


FThread(QObject *parent = 0);
virtual void run();
File *f;
void sampleFunc();
signals:
void done();


};

file.cpp:


#include "file.h"
#include <QDebug>
#include "fthread.h"

File::File(QObject *parent) :
QObject(parent)
{

connect(fthread,SIGNAL(done()),this,SLOT(print())) ;

}
void File::print(){
qDebug()<<"printing from File object";
fthread.exit();
}

fthread.cpp


#include "fthread.h"
#include "file.h"
#include <QDebug>

FThread::FThread(QObject *parent) :
QThread(parent)
{

f=new File();


}

void FThread::run(){

sampleFunc();
this->exec();

}

void FThread::sampleFunc()
{
emit done(); //<<--------------SIGNAL SHOULD BE EMITTED FROM HERE
}


main.cpp:

#include <QtCore/QCoreApplication>
#include <QDebug>
#include "fthread.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

qDebug()<<"App started";
FThread fthread;
fthread.start();
fthread.wait();
return a.exec();
}

NOTE:

i get following output:

App started
The program has unexpectedly finished.
...path/Thread exited with code 0

means it is exiting without error


any help please!!!!!!!

high_flyer
26th January 2011, 16:22
You are emitting your signal from the thread before QApplication event loop is running (before a.exc()) so that signal never gets processed by the application, and thus the slot is not called.
I am not sure why the application ends however - need to think...

MarekR22
26th January 2011, 16:57
read this (http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/), it should help.
See also (http://www.qtcentre.org/threads/37546-QThread-clarification?p=172647#post172647).

ChrisW67
26th January 2011, 23:43
i dont understand why are my signals not working in this multi threaded app

Have a look at the comments I have inserted into your code snippets


file.h:


#include <QObject>


class FThread;

class File : public QObject
{
Q_OBJECT
public:
File(QObject *parent = 0);
FThread *fthread;
// ^^^^^ Where does this pointer ever get an instance of FThread attached to it?
public slots:
void print();

};

file.cpp:

#include "file.h"
#include <QDebug>
#include "fthread.h"

File::File(QObject *parent) :
QObject(parent)
{

connect(fthread,SIGNAL(done()),this,SLOT(print())) ;
// ^^^^^ you use fthread here but is has not been initialised either 0 (NULL)
// or to be a valid pointer.
// On the way to crash city

}
void File::print(){
qDebug()<<"printing from File object";
fthread.exit();
// ^^^^^ given that fthread is a pointer I am surprised this even compiles
}


NOTE:

i get following output:

App started
The program has unexpectedly finished.
...path/Thread exited with code 0

means it is exiting without error

Which part of "The program has unexpectedly finished." means "exiting without error"?

Have you single-stepped the program to see at what point the program terminates unexpectedly?