信号和插槽示例不起作用

时间:2013-05-03 16:44:46

标签: c++ qt

尝试熟悉信号槽概念。我想出了以下代码

class Person: public QObject
{
    Q_OBJECT 
public:
    void SignalEmitter(QString Words); //This emits the signal
    Person(QObject *parent = 0);
signals:
    void Call(QString Words); //This is the signal
};

class Animal: public QObject
{
    Q_OBJECT 
public:
    Animal(QObject *parent = 0);
public slots:
    void Respond(QString Words); //This is the slot
};

class SomeClass: public QObject
{

    Q_OBJECT 
        public:
        SomeClass(QObject *parent = 0);
};

现在目标很简单 - 该人发送信号并且动物在一个插槽中接收它 这是我的实现

void Person::SignalEmitter(QString Words)
{
    //Emit the signal
    emit Call("Signal emitted");
}

void Animal::Respond(QString Words)
{
    qDebug() <<"Responding " << Words;
    std::string d= "Breakpoint should be here";
}

//这是代码

SomeClass::SomeClass(QObject *parent):QObject(parent)
{
    Person *p = new Person();
    Animal *a = new Animal();
    connect(p,SIGNAL(SignalEmitter(QString)) ,a,SLOT(Respond(QString)));
    p->SignalEmitter("Lassie");
    std::string d = "dd";
}

不幸的是,断点永远不会出现Animal::Respond(QString Words)关于我可能做错的任何建议吗?

1 个答案:

答案 0 :(得分:2)

我相信下面的SIGNAL

connect(p,SIGNAL(SignalEmitter(QString)) ,a,SLOT(Respond(QString)));

应该是

SIGNAL(Call(QString))

您将信号连接到插槽,即呼叫响应