我刚开始使用QT,我知道信号/插槽的概念,但在实现它时我遇到了问题 . 看看我的代码:
#include "test.h"
#include <QCoreApplication>
test::test()
{
// TODO Auto-generated constructor stub
}
test::~test()
{
// TODO Auto-generated destructor stub
}
void test::fireslot(){
qDebug("the slot fired");
}
void test::dosignaling(){
QObject::connect(this,SIGNAL(callslot()),this,SLOT(fireslot()));
}
注意:我已经添加了Q_OBJECT宏并从test.h中的QObject继承
这是我的测试容器
#include "test.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//test t1();
test *t2 = new test();
t2->dosignaling();
return a.exec();
}
代码编译完美但没有任何事情会发生 . 我不太确定哪个部分我犯了错误: - ?
1 回答
您在
void test::dosignaling
中的代码将插槽"fireslot"连接到信号"callslot",但您在哪里 emittingcallslot
信号?您应该更改代码并将
QObject::connect()
放在构造函数(或其他位置)中,并将dosignaling
方法更改为:此外,您尚未显示头文件,但它应包含调用信号的声明,如下所示: