如何从MainWindow关闭QApplication [英] How to Close QApplication from MainWindow

查看:226
本文介绍了如何从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.

推荐答案

我找到了解决方案。我收到sql错误的任何地方,我只是关闭表单不中止应用程序。同样使用这个链接我找到了一个很好的方式来显示我的登录对话框,因此它在成功登录之前不会显示MainWindow。



http://stackoverflow.com/questions/5527908 / qt-how-to-open-qdialog-when-main-application-done-full-to-load-all-Widgets [ ^ ]







我对建议的代码进行了此更改。你需要自己设计登录gui对话框。





I found a solution. Any place I get a sql error, I simply close the form not abort the app. Also using this link I found a nice way of showing my login dialog so it doesn't show the MainWindow before a successful login.

http://stackoverflow.com/questions/5527908/qt-how-to-open-qdialog-when-main-application-done-fully-to-load-all-widgets[^]



I made this change to the suggested code. You need to design the login gui dialog yourself.


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;


对不起,我不确定你的意思是调用accept(),reject(),或完成(int r)你能详细说明吗?从m_Login-> exec()返回的100是什么意思?你的意思是QDialog :: Done?
I'm sorry, I'm not sure what you mean by calling accept(), reject(), or done(int r) can you elaborate? Also what does the 100 returned from m_Login->exec() mean? did you mean QDialog::Done ?


这不是初始问题的解决方案,而是解决方案1提供的更好的方法,并解释了我在对该解决方案的评论中所说的内容: br />
This is not a solution for the initial question but a better approach then provided by solution 1 and explains what I said in my comment to that solution:
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'返回代码只是一个例子。在下面的代码中,我使用 QDialog QMessageBox :: Retry 代替。



登录对话框:


The '100' return code is just an example. In the below code I use QDialogQMessageBox::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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆