title: Qt: Calling MainWindow::Ui from another class using signals and slots
body: <p>I made a simple Qt project to cover the issue of calling Ui from another class.<br><br></p>
  
  <p><b>The Files:</b><br>
  mainwindow.h | mainwindow.cpp | client.h | client.cpp | main.cpp</p>
  
  <p><b>The Issue:</b><br>
  Connecting a signal from <em>client.cpp</em> to a slot in <em>mainwindow.cpp</em> worked very well.<br>
  But when I added a <pre>ui->statusBar->showMessage("message");</pre>
  in the slot, it didn't work.<br>
  NOTE: When I made the signal and the slot both in <em>mainwindow.cpp</em> it worked, but calling a slot from <em>mainwindow.cpp</em> from a signal and <em>connect()</em> in <em>client.cpp</em> doesn't work.<br><br></p>
  
  <p><b>The Codes:</b> (trimmed to the essentials)<br><br>
  <em>mainwindow.h</em></p>
  
  <pre><code>namespace Ui {
      class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  
  signals:
  
  public slots:
      void doSomething();
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
  
  
  private:
      Ui::MainWindow *ui;
  
  };
  </code></pre>
  
  <p><br>
  <em>mainwindow.cpp</em></p>
  
  <pre><code>MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
  }
  
  
  void MainWindow::doSomething()
  {
      QMessageBox::information(0, "test", "BINGO!!");
      ui-&gt;statusBar-&gt;showMessage("testing");
  }
  </code></pre>
  
  <p><br>
  <em>client.h</em></p>
  
  <pre><code>class client : public QObject
  {
      Q_OBJECT
  public:
      explicit client(QObject *parent = 0);
      void call();
  
  signals:
      void connected();
  
  public slots:
  
  };
  </code></pre>
  
  <p><br>
  <em>client.cpp</em></p>
  
  <pre><code>client::client(QObject *parent) :
      QObject(parent)
  {
      MainWindow main;
      connect(this, SIGNAL(connected()), &amp;main, SLOT(doSomething()));
      call();
  }
  
  void client::call()
  {
      emit connected();
  }
  </code></pre>
  
  <p><br><br>
  <b>Added:</b><br>
  <em>main.cpp</em></p>
  
  <pre><code>#include &lt;QtGui/QApplication&gt;
  #include "mainwindow.h"
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p>Thanks.</p>