【发布时间】:2019-07-30 13:42:54
【问题描述】:
我是 Qt 的新手。我有一个奇怪的问题。当我隐藏我的主窗口(在应用程序启动时打开)时,我的应用程序会在一段时间后自动关闭。但如果我不使用 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() 方法时:
主窗口确实隐藏了 该应用程序从 Dock 中消失 该应用程序从任务管理器中消失 (有趣的是,从 Creator 的角度来看,应用程序并没有退出。如果我单击运行按钮,它会给我“等待应用程序停止”消息。)
/* 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>
【问题讨论】:
-
这不正常或不正常。我在工作中使用 Qt 的 11 年里从未见过这种行为。
-
你的代码不完整,好像需要
"ui_mainwindow.h"。请将其简化为证明问题的minimal reproducible example;谢谢。 -
代码示例很好。在 5 分钟内,我能够创建自己的 ui 文件,并复制他的问题。任何有 Qt 设计师知识的人都可以做同样的事情,没有这些知识的人可能不应该尝试回答这个问题。
-
一个完整的例子是网站的要求。第二条评论中的链接解释了这一点。
-
好的,也许代码示例不是“很好”,但鉴于我能够重现他的问题(并确认 QWidget 表现出相同的行为),这似乎是一个合法的问题,值得解决。如果它被关闭将是一种耻辱,我们无法让更多的人看到它。当然是国际海事组织。