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

Sey*_*emi 0 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()));

}
Run Code Online (Sandbox Code Playgroud)

注意:我已经添加了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();
}
Run Code Online (Sandbox Code Playgroud)

代码编译完美但没有任何事情会发生.我不太确定哪个部分我犯了错误: - ?

Ida*_*n K 5

你拥有的代码void test::dosignaling连接槽"fireslot"发送信号"callslot",但你在哪里发射callslot信号?

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

void test::dosignaling()
{
    emit callslot();
}
Run Code Online (Sandbox Code Playgroud)

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

class test
{
    ...
signals:
    void callslot();
};
Run Code Online (Sandbox Code Playgroud)