why doesn't signal gets emiited?
i dont understand why are my signals not working in this multi threaded app
file.h:
Code:
#include <QObject>
class FThread;
{
Q_OBJECT
public:
FThread *fthread;
public slots:
void print();
};
fthread.h
Code:
#include <QThread>
class File;
{
Q_OBJECT
public:
virtual void run();
File *f;
void sampleFunc();
signals:
void done();
};
file.cpp:
Code:
#include "file.h"
#include <QDebug>
#include "fthread.h"
{
connect(fthread,SIGNAL(done()),this,SLOT(print()));
}
void File::print(){
qDebug()<<"printing from File object";
fthread.exit();
}
fthread.cpp
Code:
#include "fthread.h"
#include "file.h"
#include <QDebug>
FThread
::FThread(QObject *parent
) :{
f=new File();
}
void FThread::run(){
sampleFunc();
this->exec();
}
void FThread::sampleFunc()
{
emit done(); //<<--------------SIGNAL SHOULD BE EMITTED FROM HERE
}
main.cpp:
Code:
#include <QtCore/QCoreApplication>
#include <QDebug>
#include "fthread.h"
int main(int argc, char *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!!!!!!!
Re: why doesn't signal gets emiited?
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...
Re: why doesn't signal gets emiited?
read this, it should help.
See also.
Re: why doesn't signal gets emiited?
Quote:
Originally Posted by
naturalpsychic
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
Quote:
file.h:
Code:
#include <QObject>
class FThread;
{
Q_OBJECT
public:
FThread *fthread;
// ^^^^^ Where does this pointer ever get an instance of FThread attached to it?
public slots:
void print();
};
file.cpp:
Code:
#include "file.h"
#include <QDebug>
#include "fthread.h"
{
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?