Qt SIGNAL-SIGNAL连接新方式

时间:2019-06-11 12:45:53

标签: qt signals

直到现在我都使用旧的连接语法。今天,我想将项目更改为新的连接语法,如下所示:

#include <QDebug>

class Telefon : public QObject {
  Q_OBJECT
  public:
    void letRing() {
      qDebug() << "letRing";
      emit ring();
    }

  signals:
    void ring();
  public slots:
    void onRing() {
      qDebug() << "onRing";
    }
};

class Cable : public QObject {
  Q_OBJECT
  signals:
    void ring();
};

int main(int argc, char *argv[])
{
  Telefon *fon1 = new Telefon();
  Telefon *fon2 = new Telefon();
  Cable *cable = new Cable();

  // old connection way
  // connect(fon1,  SIGNAL(ring()), cable, SIGNAL(ring()));
  // connect(cable, SIGNAL(ring()), fon2,  SLOT(onRing()));

  // new connection way
  QObject::connect(fon1,  &Telefon::ring, cable, &Cable::ring);    // <-- This is the problem
  QObject::connect(cable, &Cable::ring,   fon2,  &Telefon::onRing);

  fon1->letRing();

  return 0;
}

编译顺利,没有错误。但是电话永远不会响了:(

输出应为:

letRing
onRing

但这是

letRing

我深入研究Qt代码,发现SIGNAL-SLOT连接有效,但SIGNAL-SIGNAL连接无法正常工作。除此之外,编译时不会产生错误。有没有办法以新的方式/语法连接信号?

0 个答案:

没有答案