title: Qt Model/View/Controller Example
body: <p>I am just getting started in Qt, and trying to get a simplified, working example of the model-view-controller design pattern.</p>
  
  <p>So far, I have been able to use signals and slots to connect basic widgets like push buttons to a <code>QLabel</code>, and have the view be modified as the push button is clicked/released.  See code below for a working example of that (implemented in the <code>MainWindow</code> class).</p>
  
  <p>I am trying to define a class, in this case, <code>Game</code>, which is going to be my model.  I want <code>Game</code> to have all of the data and business rules of my entire application.  I do not require that <code>Game</code> be anything Qt specific--it very well could be generic C++.  However, in the code below, it does have some Qt-specific code to implement a <code>QTimer</code> which is useful for the purposes of this example.</p>
  
  <p>I am trying to achieve two things in this example:</p>
  
  <ol>
  <li>I want to have a model which is able to generate some sort of event within itself, like incrementing a variable value over the passage of time, and then ultimately see that change somehow reflected in the view.  Or better yet, the <code>timeout()</code> of the <code>QTimer</code> could simply be the signal that is connected to some slot, that slot being some event that takes place within the model.  Using the code shown below, the reflection in the view would be the setting of <code>label_1</code> (part of the <code>MainWindow</code> class) to display one of the images already stored in <code>imageOn</code> or <code>imageOff</code> (also part of the <code>MainWindow</code> class).</li>
  <li>I want to have the push button associated with the <code>on_pushButton_clicked()</code> and <code>on_pushButton_pressed()</code> slots be able to modify some value stored within the model.  Then, coming full circle with item 1, have that update of the model be reflected in the view.</li>
  </ol>
  
  <p>If my terminology thus far is incorrect or inconsistent with Qt terminology of MVC design pattern, forgive me.  I would welcome any clarification on that.  Also, if the example code I have provided is too convoluted for exemplifying the MVC design pattern in Qt, I am more than willing to wipe the slate clean and start with a more appropriate example.  All I am trying to do is get started with Qt and MVC, but in a way that deals with more complex data types.</p>
  
  <p>I am trying to develop an example in which I can handle a model and class such as <code>Game</code> which is potentially complex--not a simple list of QStrings or something guaranteed to be more straight-forward.  When I browsed through the Qt documentation related to MVC, I came across a lot of examples that used the <code>setModel()</code> function to try and make the connections I am essentially outlining in list items 1 and 2.  The problem was that I could not see a way of using that exact approach with a more complex data-type like <code>Game</code> which might be the entire data model for a complete application (I know <code>Game</code> is not complex in this example, but it could be eventually).  I need something that is scalable and extensible, something that would work for an entire application.  If those <code>setModel()</code>-type functions are suitable for this--which they very likely could be, I just could not figure it out on my own--I would like to know how to implement those in this example dealing with <code>QLabel</code> and images.</p>
  
  <p>Code:</p>
  
  <p><strong>game.h</strong></p>
  
  <pre><code>#ifndef GAME_H
  #define GAME_H
  
  #include &lt;QtCore&gt;
  
  class Game : public QObject {
  
      Q_OBJECT
  
  public:
      Game();
      void timed_job();
  
  private:
      QTimer *timer;
  };
  
  #endif // GAME_H
  </code></pre>
  
  <p><strong>game.cpp</strong></p>
  
  <pre><code>#include "game.h"
  #include &lt;QtCore&gt;
  
  Game::Game() {
  }
  
  void Game::timed_job() {
      timer = new QTimer(this);
      timer-&gt;start(1000);
      //connect(timer, SIGNAL(timeout()), this, SLOT(flip()));
  }
  </code></pre>
  
  <p><strong>mainwindow.h</strong></p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  
  namespace Ui {
      class MainWindow;
  }
  
  class MainWindow : public QMainWindow {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
  
  private slots:
      void on_pushButton_clicked();
      void on_pushButton_pressed();
  
  private:
      Ui::MainWindow *ui;
      QImage imageOn, imageOff;
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p><strong>mainwindow.cpp</strong></p>
  
  <pre><code>#include &lt;QImage&gt;
  #include "mainwindow.h"
  #include "ui_mainwindow.h"
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow) {
      imageOn.load(":/Files/On.jpg");
      imageOff.load(":/Files/Off.jpg");
  
      ui-&gt;setupUi(this);
  }
  
  MainWindow::~MainWindow() {
      delete ui;
  }
  
  void MainWindow::on_pushButton_clicked() {
      ui-&gt;label_1-&gt;setPixmap(QPixmap::fromImage(imageOff));
  }
  
  void MainWindow::on_pushButton_pressed() {
      ui-&gt;label_1-&gt;setPixmap(QPixmap::fromImage(imageOn));
  }
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;QtGui/QApplication&gt;
  #include &lt;QLabel&gt;
  #include "mainwindow.h"
  #include "game.h"
  
  int main(int argc, char *argv[]) {
      QApplication a(argc, argv);
  
      MainWindow w;
  
      w.show();
  
      return a.exec();
  }
  </code></pre>