当MainWindow隐藏时,QT应用程序会自动退出

Qt application exit automatically when mainWindow is hidden

本文关键字:退出 应用程序 QT MainWindow 隐藏      更新时间:2023-10-16

我是QT的新手。我有一个奇怪的问题。当我隐藏MainWindow(应用程序启动时打开)时,我的应用程序会自动关闭。但是,如果我不使用this-> hide()应用程序可以正常工作。

mainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "aclogin.h"
#include "atm.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void on_pushButton_bank_clicked();
    void on_pushButton_atm_clicked();
private:
    Ui::MainWindow *ui;
    aclogin *_login;
    atm *_atmEnter;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_bank_clicked()
{
    this->hide();
    _login = new aclogin(this);
    _login->setWindowTitle("Log In");
    _login->setModal(true);
    _login->show();
}
void MainWindow::on_pushButton_atm_clicked()
{
    this->hide();
    _atmEnter = new atm(this);
    _atmEnter->setWindowTitle("ATM");
    _atmEnter->setModal(true);
    _atmEnter->show();
}

main.cpp

#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow *_BankSystem = new MainWindow();
    _BankSystem->setWindowTitle("Bank System");
    _BankSystem->show();
    return a.exec();
}

但是,如果我隐藏其他窗口,应用程序正常。谢谢。

删除了代码,并添加了一个.UI文件。OP指示的行为是正确的。执行hide()方法:

主窗口确实确实隐藏了该应用从码头消失该应用程序从任务管理器中消失(有趣的是,从创建者的角度来看,该应用程序尚未退出。如果我单击"运行"按钮,它会给我"等待应用程序停止"消息。)

/* mainwindow.h */

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void on_pushButton_bank_clicked();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

/* mainwindow.c */

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_bank_clicked()
{
    this->hide();
}

/* main.c */

#include "mainwindow.h"
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow *_BankSystem = new MainWindow();
    _BankSystem->show();
    return a.exec();
}

这是.ui文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QPushButton" name="atm">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>30</y>
     <width>80</width>
     <height>18</height>
    </rect>
   </property>
   <property name="text">
    <string>atm</string>
   </property>
  </widget>
  <widget class="QPushButton" name="bank">
   <property name="geometry">
    <rect>
     <x>230</x>
     <y>30</y>
     <width>80</width>
     <height>18</height>
    </rect>
   </property>
   <property name="text">
    <string>bank</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

qguiapplication

quitonlastwindowclated:bool此属性保留该应用程序是否在关闭最后一个窗口时隐式退出。

默认值为真。

如果此属性为真,则应用程序在最后一个可见的主窗口(即没有父母的窗口)时退出。

访问功能:

bool    quitOnLastWindowClosed()
void    setQuitOnLastWindowClosed(bool quit)

也有MainWindow *_BankSystem = new MainWindow();的内存泄漏。您可以通过将父添加到new MainWindow(),删除或智能指针来解决。

使用QApplication::setQuitOnLastWindowClosed作为a.setQuitOnLastWindowClosed(false),以防止应用程序终止是否关闭。