qApp->quit() from other class not working



  • Hey there,

    when I start my application I check a catalog of files if any file is missing. If there is any file missing, I show a custom messagebox (just a class which inherits from QMessageBox) to the user. In this messagebox the user has the ability to click on Exit, to quit the whole application. This is how I achieve it:

            QAbstractButton *openAnywayBtn;
            QAbstractButton *exitBtn;
            // -----------------------------
            QAbstractButton *showListBtn;
            QAbstractButton *deleteFromCatBtn;
            if (missingFiles.count() >= 20) {
                box->setBoxDescription(tr("There are more than 20 files missing in your catalog."));
                showListBtn = box->addButton(tr("Show Detailed List"), QMessageBox::ResetRole);
                openAnywayBtn = box->addButton(tr("Open anyway"), QMessageBox::YesRole);
                exitBtn = box->addButton(tr("Exit"), QMessageBox::NoRole);
            } else {
                box->setBoxDescription(_msg + _files);
                openAnywayBtn = box->addButton(tr("Open anyway"), QMessageBox::ResetRole);
                deleteFromCatBtn = box->addButton(tr("Delete from Catalog"), QMessageBox::YesRole);
                exitBtn = box->addButton(tr("Exit"), QMessageBox::NoRole);
            }
            box->exec();
            if (box->clickedButton() == exitBtn) {
                qApp->quit();
            }
    

    My main.cpp just looks like this:

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        CatalogFileList *list = new CatalogFileList("C:/Users/Jan/Desktop/DefaultCatalog");
    
        QStringList catalogFiles;
        if (list->readXml()) {
            if (list->checkFiles()) { // the check is included in this function
                catalogFiles = list->getFiles();
            }
        }
    
        QMainWindow w;
        w.showMaximized();
    
        return app.exec();
    }
    

    When the user clicks on Exit, nothing happens. The window is show and the QApplication instance is running normally. How can I achieve to close the whole application from this MessageBox. I tried out many ways (just like QObject::connect with the slot of qApp).

    Many thanks,
    Jan



  • Hi, try your connect this way:

    connect(exitBtn, SIGNAL(clicked()), &qApp, SLOT(quit()), Qt::QueuedConnection);
    

    You need the QueuedConnection flavor to survive the app.exec() call, for more see here


Log in to reply