How to exit app?



  • I create a button and connet to slot exitApp;

     QPushButton *exitbutton  = new QPushButton(tr("exit"),menu);
     connect(exitbutton, SIGNAL(clicked(bool)),this,SLOT(exitApp()));
    
    void GameView::exitApp()
    {
        qDebug()<<"exitApp";
        QCoreApplication::exit(0);
    }
    

    but what is the matter is I must click exitbutton twice,the app can exit;In other words,APP will debug "exitApp" twice;



  • @chaochao

    May be you can try this...!

    qApp->closeAllWindows();


  • Moderators

    @chaochao
    have you reimplemented any closeEvent() handlers in your application or smiliar?



  • @Rohith it can close current window,but it will display a untitled window. i do not know where the untitled window came from



  • @raven-worx yes ,i have reimpemented closeEvent.like this```
    void GameView::closeEvent(QCloseEvent *event)
    {
    if(_isGameStart)
    {
    int r = QMessageBox::warning(this,tr("Close"),
    tr("Score is %1 \n"
    "Would you want to close game?").arg(_score->toPlainText().toInt()),
    QMessageBox::Ok|QMessageBox::Cancel);
    if(r==QMessageBox::Ok)
    event->accept();
    else
    event->ignore();
    }
    else
    event->ignore();
    }

    but it  belongs to GameView; code in the question is in a dialog.
    whole project like this:
    class GameView:public QGraphicsView
    {
    public:
        GameView();
     void initGame();
    }
    in .cpp:
    GameView::GameView()
        :_isGameStart(false),_gameLevel(1)
    { 
        initGame();
    }
    void GameView::initGame()
    {
         QDialog *menu = new QDialog(this);
        QPushButton *exitbutton  = new QPushButton(tr("Exit"),menu);
       connect(exitbutton, SIGNAL(clicked(bool)),this,SLOT(exitApp()));
    }

  • Moderators

    @chaochao said:

    void GameView::initGame()
    {
    QDialog *menu = new QDialog(this);
    QPushButton *exitbutton = new QPushButton(tr("Exit"),menu);
    connect(exitbutton, SIGNAL(clicked(bool)),this,SLOT(exitApp()));
    }

    this code doesn't make sense.
    You are creating a dangling QDialog (window) but never show it.



  • @raven-worx i also find it.but how to solve?


  • Moderators

    @chaochao
    well when you never show it you can simply remove the code ;)



  • @raven-worx remove?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.