无法将信号绑定到QT中的插槽

时间:2010-01-05 10:48:51

标签: qt signals-slots

我刚刚开始使用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 个答案:

答案 0 :(得分:5)

void test::dosignaling中的代码将插槽“fireslot”连接到信号“callslot”,但是发出 callslot信号在哪里?

您应该更改代码并将QObject::connect()放置在构造函数(或其他位置)中,并将dosignaling方法更改为:

void test::dosignaling()
{
    emit callslot();
}

此外,您尚未显示头文件,但它应包含调用信号的声明,如下所示:

class test
{
    ...
signals:
    void callslot();
};
相关问题