我的班级有一个名为mythread的朋友类成员,我在MyThread类的run方法中发出了父类的信号,但没有发生任何事情,并且连接到myclass的插槽没有调用。
class ActorT : public QObject {
Q_OBJECT
public:
ActorT() { thre = new MyThread(this); }
signals:
void sendTest();
public slots:
void start() { thre->start(); }
void wait() { thre->wait(); }
private:
MyThread* thre;
};
class MyThread : public QThread {
public:
MyThread(ActorT* parent) { mc = parent }
void run() {
for(int i=0; i<100; i++)
{
emit mc->sendTest();
msleep(10);
}
}
ActorT* mc;
};
class Test : public QObject {
Q_OBJECT
public:
Test() { i = 0; }
public slots:
void times() { printf("%d \n", ++i); }
private:
int i;
}
int main() {
QCoreApplication app;
ActorT a;
Test t;
QObject::connect(&a, SIGNAL(sendTest()), &t, SLOT(times()));
a.start();
a.wait(); // this line is wrong part
return app.exec();
}
我不明白这个实现有什么问题?
更新:我现在知道什么是错的,非常感谢。