1

I tried to use qApp->exit() to exit application and close UI. but I failed the UI is still there after qApp->exit() executed. Anyone can help to figure out why? thanks a lot.

#include "clsDownloadUpdateList.h"
#include <QApplication>
#include <qtranslator.h>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTranslator translator;
    translator.load("en-CN_upgrader");
    qApp->installTranslator(&translator);
    clsDownloadUpdateList w;
    w.show();

    return a.exec();
}

clsDownloadUpdateList::clsDownloadUpdateList(QWidget *parent) :
    QMainWindow(parent),
    _state(STOP),
    ui(new Ui::clsDownloadUpdateList)
{
    ui->setupUi(this);
    this->setWindowTitle("GCS Upgrader");
// other code
// here comes the code to exit application
            qApp->exit();
// but the UI is still there.
}
Nicholas Yu
  • 333
  • 1
  • 5
  • 7

3 Answers3

4

@thuga is right. The problem you have is caused by your wrong code: you call qApp->exit() before right in your constructor, where your application has not started it's message cycle yet (by a.exec()).

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTranslator translator;
    translator.load("en-CN_upgrader");
    qApp->installTranslator(&translator);
    clsDownloadUpdateList w; // <- you call qApp->exit() right here, before a.exec();
    w.show();

    return a.exec();
}
VP.
  • 15,509
  • 17
  • 91
  • 161
  • thanks! Now I understand why it does not work. The reason I want to exit the application is in the constructor code, I did check whether another related application is opened using QsharedMemory. In this case, if the related application is opened. It will open up a dialog telling user another application is open and whether he want to close this application. Once he clicked "Yes", it will exit the application. So in this case, where should I implement this checking Qsharememory and exit application logic? In the main, probably after clsDownloadUpdateList w; but before w.show()? thanks – Nicholas Yu Jan 24 '16 at 05:07
  • Am I wrong in understanding your goal? - You want this application to check if some related application is already running. If it is already running - show dialog box with a question "Do you want to close the related application? (Yes, No)" - and if "Yes" - close it? – VP. Jan 24 '16 at 10:27
  • Yes, Victor you totally understands me. So where should I implement this logic? – Nicholas Yu Jan 25 '16 at 01:32
2

It will not help you in constructor, because of no event loop started yet.

In such case you can use QTimer::singleShot() with timeout equal zero. It will cause calling what you need when event loop started. Also it is good idea to use initialization method and check it in main:

Window w;
if ( !w.init() )
   return 1;
w.show();
return a.exec();
Jeka
  • 1,364
  • 17
  • 33
0

Working code:

#include <QMetaObject>
//...
QMetaObject::invokeMethod(qApp, "quit",
    Qt::QueuedConnection);

Or for widgets:

QMetaObject::invokeMethod(this, "close",
    Qt::QueuedConnection);
Vladimir Bershov
  • 2,701
  • 2
  • 21
  • 51