How to exit a "game" in Qt when health is 0
<p>I'm quite new to Qt, but I got into it to start experimenting with making 2D games. I've got a extremely rough and simple game started, but I have an issue. Whenever the health gets to 0, the game doesn't end. I just want to know how to end the game and where to put this exit command before making a "Game Over" screen. My code is below, and from what I can grasp, I assume the QApplication::quit() goes in the Game.cpp file. Doing this by taking the health integer from Health.cpp and Health.h and putting it in Game.cpp. Any help is appreciated. Here is the code I feel the answers lie in, if more info is needed, ask.</p>

<p><strong>Game.h</strong></p>

<pre><code>#ifndef GAME_H
#define GAME_H

#include &lt;QGraphicsView&gt;
#include &lt;QWidget&gt;
#include &lt;QGraphicsScene&gt;
#include "Player.h"
#include "Score.h"
#include "Health.h"
#include "Level.h"
#include "Main.h"

class Game: public QGraphicsView{
public:
    Game(QWidget * parent=0);

    QGraphicsScene * scene;
    Player * player;
    Score * score;
    Health * health;
    Level * level;
    Main * close;
    int end();
};

#endif // GAME_H
</code></pre>

<p><strong>Game.cpp</strong></p>

<pre><code>#include "Game.h" 
#include &lt;QTimer&gt;
#include &lt;QGraphicsTextItem&gt;
#include &lt;QFont&gt;
#include "Enemy.h"
#include &lt;QMediaPlayer&gt;
#include &lt;QBrush&gt;
#include &lt;QImage&gt;
#include &lt;QApplication&gt;

Game::Game(QWidget *parent){
    // create the scene
    scene = new QGraphicsScene();
    scene-&gt;setSceneRect(0,0,800,600); // make the scene 800x600        instead of infinity by infinity (default)
    setBackgroundBrush(QBrush(QImage(":/images/bg.png")));

    // make the newly created scene the scene to visualize (since Game is a QGraphicsView Widget,
    // it can be used to visualize scenes)
    setScene(scene);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setFixedSize(800,600);

    // create the player
    player = new Player();
    player-&gt;setPos(400,500); // TODO generalize to always be in the     middle bottom of screen
    // make the player focusable and set it to be the current focus
    player-&gt;setFlag(QGraphicsItem::ItemIsFocusable);
    player-&gt;setFocus();
    // add the player to the scene
    scene-&gt;addItem(player);

    // create the score/health
    score = new Score();
    scene-&gt;addItem(score);
    health = new Health();
    health-&gt;setPos(health-&gt;x(),health-&gt;y()+25);
    scene-&gt;addItem(health);
    level = new Level();
    scene-&gt;addItem(level);Bull
    level-&gt;setPos(level-&gt;x(),level-&gt;y()+50);

    // spawn enemies
    QTimer * timer = new QTimer();
    QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn()));
    timer-&gt;start(2000);

    // play background music
    QMediaPlayer * music = new QMediaPlayer();
    music-&gt;setMedia(QUrl("qrc:/sounds/bgsound.mp3"));
    music-&gt;play();

    show();
}

int Game::end(){
    if (health == 0){
        QApplication::quit();
    }
    return 0;
}
</code></pre>

<p><strong>Health.h</strong></p>

<pre><code>#ifndef HEALTH_H
#define HEALTH_H

#include &lt;QGraphicsTextItem&gt;

class Health: public QGraphicsTextItem{
public:
    Health(QGraphicsItem * parent=0);
    void decrease();
    int getHealth();
private:
    int health;
};

#endif // HEALTH_H
</code></pre>

<p><strong>Health.cpp</strong></p>

<pre><code>#include "Health.h"
#include &lt;QFont&gt;
#include &lt;QApplication&gt;

Health::Health(QGraphicsItem *parent): QGraphicsTextItem(parent){
    // initialize the score to 0
    health = 3;

    // draw the text
    setPlainText(QString("Health: ") + QString::number(health)); //   Health: 3
    setDefaultTextColor(Qt::red);
    setFont(QFont("times",16));
}

void Health::decrease(){
    health--;
    setPlainText(QString("Health: ") + QString::number(health)); //     Health: 2
}

int Health::getHealth(){
    return health;
}
</code></pre>

<p><strong>main.cpp</strong></p>

<pre><code>#include &lt;QApplication&gt;
#include "Game.h"
#include "Main.h"

Game * game;

int main(int argc, char *argv[]){
    QApplication a(argc, argv);

    game = new Game();
    game-&gt;show();

    return a.exec();
}
</code></pre>