title: How to connect signal and slot in different classes in Qt?
body: <p>I have two simple <code>classes</code>(<code>class A</code> and <code>class B</code>).<br>
  In <code>a.h</code>, I just declared a <code>QPushButton</code>:<br></p>
  
  <pre><code>QPushButton *testBtn = new QPushButton(this);
  </code></pre>
  
  <p>In <code>b.h</code>:</p>
  
  <pre><code>class B : public QMainWindow
  {
      Q_OBJECT
  public:
      explicit B(QWidget *parent = nullptr);
      A testingA;
  public slots:
      void testing();
  };
  </code></pre>
  
  <p>and <code>b.cpp</code>:</p>
  
  <pre><code>B::B(QWidget *parent) : QMainWindow(parent)
  {
      connect(testingA.testBtn, &amp;QPushButton::clicked, this, &amp;B::testing);
  }
  
  void B::testing()
  {
      qDebug() &lt;&lt; "testing";
  }
  </code></pre>
  
  <p>I am trying to connect the <code>signal</code> in <code>class A</code> to the <code>slot</code> in <code>class B</code>, but from the code I provide, it's not working. <br><br>
  So what is the right way to do it? <br>Thanks </p>
  
  <p><b>Edit:</b><br>
  According to PRIME's answer, I made a few changes. <br>
  In A's constructor, added:</p>
  
  <pre><code>connect(testBtn, &amp;QPushButton::clicked, [this](){OnButtonClicked();});
  </code></pre>
  
  <p>to emit the own defined <code>OnButtonClicked()</code> <code>signal</code>;<br><br>
  and in B's construtor, changed to this:</p>
  
  <pre><code>connect(&amp;testingA, &amp;A::OnButtonClicked, this, &amp;B::testing); 
  </code></pre>
  
  <p>But when I clicked the button, the <code>testing</code> <code>slot</code> still not triggered.<br></p>
  
  <p><b>Edit 2:</b><br>
  After doing some researches and trying a few times, I found that if I created <code>B</code>'s <code>object</code> in <code>A</code>'s <code>constructor</code>, and then connect <code>A</code>'s signal to <code>B</code>'s <code>slot</code> in <code>A</code>, it will work. <br>
  But I really can not figure out why I can not connect <code>A</code>'s signal to <code>B</code>'s <code>slot</code> in <code>B</code>. <br>
  This is what's in the <code>main.cpp</code>:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      B b;
      A w;
      w.show();
      return a.exec();
  }
  </code></pre>
  
  <p>Is that because of some reasons that <code>A</code>'s object is out of scope in <code>B</code>?<br>
  Can someone tell me where I did wrong? Thanks so much.</p>