[Qt-interest] can't send signal in multithread
Mihail Naydenov
mlists at ymail.com
Thu Apr 15 11:03:14 CEST 2010
You'd better search the archives, this has been discussed thousand times...
At a first glance it seams wrong you are waiting for the thread before app exec.
a.exec will never be reached and the signal to jm cannot be delivered.
I might be wrong, my thread skills are rusty at the moment,
but as I said, search the list, you will find the answer for sure!
MihailNaydenov
>
>From: beka nosky <bekanosky at gmail.com>
>To: Qt-interest at trolltech.com
>Sent: Thu, April 15, 2010 11:08:38 AM
>Subject: [Qt-interest] can't send signal in multithread
>
>>Hi All,
>I make some simple code to send signal in multithread environment. But it failed.
>The code was successfull in non-threading environment.
>
>This is my code. Where is my fault ?
>
>/*------------------------------------------------------------*/
>>main.cpp
>/*------------------------------------------------------------*/
>#include <QtCore/QCoreApplication>
>#include "JobMgr.h"
>
>int main(int argc, char *argv[])
>{
> QCoreApplication a(argc, argv);
>
> JobMgr jm;
>
> jm.start();
> jm.wait();
>
> return a.exec();
>}
>
>/*------------------------------------------------------------*/
>>JobMgr.h
>>/*------------------------------------------------------------*/
>
>#ifndef JOBMGR_H
>#define JOBMGR_H
>
>#include <QThread>
>#include <QFileSystemWatcher>
>
>class JobMgr : public QThread
>{
>Q_OBJECT
>public:
> explicit JobMgr(QObject *parent = 0);
>> void run();
>
>private:
> QFileSystemWatcher fsw;
>signals:
>
>public slots:
> void dirChangedHandler(QString dir_path);
>};
>
>#endif // JOBMGR_H
>
>
>
>>/*------------------------------------------------------------*/
>>
>JobMgr.cpp
>>
>/*------------------------------------------------------------*/
>
>#include "JobMgr.h"
>#include <stdio.h>
>
>JobMgr::JobMgr(QObject *parent) :
> QThread(parent)
>{
>}
>
>void JobMgr::run()
>{
> fsw.addPath("/tmp/job/");
>
> if( connect(&fsw,SIGNAL(directoryChanged(QString)),SLOT(dirChangedHandler(QString))) == false) {
>> fprintf(stderr,"%s connect error\n",__func__);
> }
> else
> fprintf(stderr,"%s connect succeed\n",__func__);
>
> fflush(stderr);
>
> exec();
>}
>
>void JobMgr::dirChangedHandler(QString dirPath)
>>{
> fprintf(stderr,"%s() ada perubahan di dir :%s\n",__func__, dirPath.toStdString().c_str());
> fflush(stderr);
>}
>
>
More information about the Qt-interest-old
mailing list