1

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

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)对我可能做错的事情提出任何建议?

4

1 回答 1

2

我相信下面的信号

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

应该

SIGNAL(Call(QString))

您将信号连接到插槽,即 Call to Respond

于 2013-05-03T16:54:04.973 回答