我面临着一个奇怪的问题,而试图执行以下程序QT信号插槽不工作
main.cpp
#include "sample.h"
#include <QList>
#include <unistd.h>
int main(int argc, char **argv)
{
A a;
a.callA();
while(1)
sleep(1);
return 0;
}
sample.cpp的
#include "sample.h"
#include <QList>
#include <QMetaMethod>
#include <unistd.h>
Thread::Thread(A *a)
{
}
void Thread::run()
{
int i = 5;
while (i){
qDebug()<< i;
sleep(2);
i--;
}
emit processingDone(">>> FROM THREAD");
qDebug()<<"Emited signal from thread";
}
void A::callA()
{
qDebug()<<"from callA";
//moveToThread(thread);
thread->start();
//thread->run();
//emit signalA(">>> FROM CallA");
}
void A::slotA(QString arg)
{
qDebug()<<"from sloatA "<< arg;
}
sample.h
class A;
class Thread : public QThread
{
Q_OBJECT
public:
Thread(A *a);
~Thread(){
qDebug()<<"Thread is destroyed";
}
void run();
signals:
void processingDone(QString arg);
};
class A : public QObject{
Q_OBJECT
public:
A(){
qDebug()<<"Registering";
thread = new Thread(this);
connect(thread, SIGNAL(processingDone(QString)), this, SLOT(slotA(QString)));
connect(this,SIGNAL(signalA(QString)), this, SLOT(slotA(QString)));
}
public slots:
void callA();
void slotA(QString arg);
signals:
void signalA(QString arg);
private:
Thread *thread;
};
当我尝试执行程序时,插槽没有被调用? 如果我把moveToThraed(),那么代码将工作,但不会为我的puprose服务。我错过了什么?
我没有得到你。 – Griffin
你救了我一天 – Griffin
exec是否在主线程中运行? – Griffin