如何从MainWindow关闭QApplication

如何从MainWindow关闭QApplication

本文介绍了如何从MainWindow关闭QApplication的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个QT gui app。我的应用程序有一个窗口,其中包含一个组合框,其中包含用户可以选择编辑的记录ID。我在MainWindow类中有代码执行sql查询来填充组合框。我有代码来检查sql错误。在通知用户有错误后我想关闭整个应用程序。我用谷歌搜索了几个小时。我在MainWindow类中尝试过close,QCoreApplication :: exit()并且不起作用。请帮忙。

I writing a QT gui app. My App has a window with a combo box with the id of records that a user can pick to edit. I have code in the MainWindow class that executes a sql query to populate the combo box. I have code to check for sql errors. After informing the user that there was an error I want to shutdown the whole app. I googled this for hours. I've tried close, QCoreApplication::exit() in the MainWindow class and that doesn't work. Please help.

推荐答案

void MainWindow::showDialog()
{
    this->hide();
    TryAgain=true;
    while (TryAgain==true)
    {
        mLogin =new LoginDialog(this);

        mLogin->exec();

    }



    mLogin->close();
    if (Abort==true)
    {

        qApp->closeAllWindows();

    }
    else this->show();
}





这是我的登录对话框中的代码





Here is the code in my login dialog

#include "logindialog.h"
#include "ui_logindialog.h"
#include <mainwindow.h>
#include <menu.h>
#include <QApplication>
#include <QDebug>
#include <QString>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QMessageBox>
#include "Globals.h"




 LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
    ui->txtUser->setText("root");
    ui->txtPwd->setText("moonpie");



}

LoginDialog::~LoginDialog()
{
    delete ui;
}



void LoginDialog::on_btnCancel_clicked()
{
    TryAgain=false;
    close();
}

void LoginDialog::on_btnLogin_clicked()
{
    QString user,pwd;
    QSqlDatabase db;
    user=ui->txtUser->text();
    pwd=ui->txtPwd->text();
    bool loggedIn=false;

    qDebug() << "inside createConnection";
    db = QSqlDatabase::addDatabase("QMYSQL");
    QString connection;
    connection = db.connectionName();
    db.setHostName("localhost");
    db.setDatabaseName("mydb");
    db.setUserName(user);
    db.setPassword(pwd);



      TryAgain=false;


    if (!db.open()) {

        if (QMessageBox::question(this, "Login Failed",
        "Login Failed Retry?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
        {
            loggedIn=false;
            TryAgain=true;
            Abort=false;

        }
        else
        {
            loggedIn=false;
            TryAgain=false;
            Abort=true;
            qDebug() << "LoginDialog Abort==true";

        }

    }
    else
    {
     loggedIn= true;
     Abort = false;
    }



    if (loggedIn==false)
    {

        this->close();
    }
    else
    {
        TryAgain=false;
        this->close();





    }

qDebug() << "LoginDialog End of Login doialog";
if (TryAgain==true)
    qDebug() << "LoginDialog TryAgain==true";
else
    qDebug() << "LoginDialog TryAgain==false";

if (Abort==true)
qDebug() << "LoginDialog Abort==true";
else
    qDebug() << "LoginDialog Abort==false";
}







这是Globals.h






Here is the Globals.h

#ifndef GLOBALS_H
#define GLOBALS_H

#endif // GLOBALS_H
extern bool TryAgain;
extern bool Abort;







这是Globals.cpp






Here is the Globals.cpp

#include "Globals.h"
bool TryAgain=false;
bool Abort=false;



Quote:

这是一个糟糕的解决方案,因为它使用全局变量而不是对话框返回值。



在您的LoginDialog中根据结果调用accept(),reject()或done(int r)(这些也会关闭对话框):

accept()或done(QDialog :: Accepted):登录

reject()或完成(QDialog :: Rejected):未记录,没有中止

done( 100):未登录,abort



在主窗口中检查m_Login-> exec()返回的值。它是QDialog :: Accepted,QDialog :: Rejected,或100。

This is a bad solution because it uses global variables instead of dialog return values.

In your LoginDialog call accept(), reject(), or done(int r) according to the result like (these will also close the dialog):
accept() or done (QDialog::Accepted): Logged in
reject() or done (QDialog::Rejected): Not logged, no abort
done(100): Not logged in, abort

In your main window check the value returned by m_Login->exec(). It is QDialog::Accepted, QDialog::Rejected, or 100.



'100'返回代码只是一个例子。在下面的代码中,我使用 QMessageBox :: Retry 代替。



登录对话框:


The '100' return code is just an example. In the below code I use QMessageBox::Retry instead.

The login dialog:

// This may be removed when using the default handling (connecting Cancel button to reject())
void LoginDialog::on_btnCancel_clicked()
{
    reject();
}

void LoginDialog::on_btnLogin_clicked()
{
    // trying to open database
    if (!db.open()) {
         if (QMessageBox::question(this, "Login Failed",
        "Login Failed Retry?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
        {
            done(QMessageBox::Retry);
        }
        else
        {
            reject(); // same as done(QDialog::Rejected)
        }
    }
    else
    {
        accept(); // same as done(QDialog::Accepted)
    }
 }





MainWindow功能:



The MainWindow function:

void MainWindow::showDialog()
{
    this->hide();
    int result = QMessageBox::Retry;
    while (result == QMessageBox::Retry)
    {
        LoginDialog *login = new LoginDialog(this);
        result = login->exec();
    // EDIT: Delete dialog here if it has not been created with  Qt::WA_DeleteOnClose
        delete login;
    }
    if (result == QDialog::Rejected)
        qApp->closeAllWindows();
    else
        this->show();
}





[更新]

一个更好的解决方案是处理内部重试登录对话框:



[UPDATE]
An even better solution would be to handle retry inside the login dialog:

void LoginDialog::on_btnLogin_clicked()
{
    // trying to open database
    if (!db.open()) {
         if (QMessageBox::question(this, "Login Failed",
        "Login Failed Retry?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
        {
            // Retry: Let the dialog stay open.
            // Optionally clear the input fields here.
            return;
        }
        // Let exec() return with QDialog::Rejected
        reject();
    }
    else
    {
        // Let exec() return with QDialog::Accepted
        accept();
    }
}



相应的MainWindow函数:


The corresponding MainWindow function:

void MainWindow::showDialog()
{
    this->hide();
    LoginDialog *login = new LoginDialog(this);
    int result = login->exec();
    // Delete dialog here if it has not been created with  Qt::WA_DeleteOnClose
    delete login;
    if (result == QDialog::Rejected)
        qApp->closeAllWindows();
    else
        this->show();
}


这篇关于如何从MainWindow关闭QApplication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 10:16