当MainWindow隐藏时,QT应用程序会自动退出
Qt application exit automatically when mainWindow is hidden
我是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)
,以防止应用程序终止是否关闭。
相关文章:
- C++控制台应用程序阻止退出
- boost::asio::io_service 在线程中,不会在应用程序退出时退出
- 我在哪里编写在退出 C++ 窗口窗体应用程序之前执行的代码部分
- 正确退出 gtkmm 应用程序
- OpenGL 应用程序退出,退出代码为 -1073741515 (0xC0000135)
- 即使主窗口关闭,也不会退出应用程序
- 对 QMainWindow 使用 std::unique_ptr 时退出时出现 QT 应用程序分段错误
- 我如何使Ctrl Q退出一个没有Menubar的QT5应用程序
- C++11 监视器类,测试应用程序不想退出
- 仅在我退出应用程序后,使用Cout.write()函数在控制台上打印字符串
- 当我要退出应用程序时,如何在QT中自动调用destructor
- 在另一个线程正在运行的情况下退出应用程序时出错
- 在退出应用程序时安全退出Qt线程
- c++允许后台线程在退出应用程序之前完成
- QT/C++,OS X:退出应用程序时可选择隐藏(),以便应用程序继续运行,并在单击dock中的图标后再次显示
- 退出应用程序时崩溃
- 在某些设备上退出应用程序后仍保留进程
- 退出应用程序前关闭线程
- Win32 c++ openprocess应该返回null,如果用户已经退出应用程序,但没有
- 日志文件不可见,除非用户退出应用程序