Qt::SplashScreen 设置后无法正常结束程序进程
当窗口设置 Qt::SplashScreen 标志位后 close() 无法正常结束程序进程
- 设置 Qt::SplashScreen 有什么效果?指定这个窗口为启动动画窗口,具体表现为启动程序在任务栏没有图标显示;程序没有标题栏。
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowFlags(Qt::SplashScreen);
}
Widget::~Widget()
{
delete ui;
}
- 因为没有标题栏,所以也没有默认的 关闭 按钮。所以我们使用一个按钮来实现 关闭 的效果。
右键添加槽函数后
void Widget::on_pushButton_clicked()
{
this->close();
}
- 运行程序,此时问题暴露出来了。点击关闭按钮,虽然看不到程序了但是程序的进程还是在运行中。并没有达到想要的效果。
- 解决办法
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowFlags(Qt::SplashScreen);
this->setAttribute(Qt::WA_QuitOnClose, true); //设置 Qt::WA_QuitOnClose属性为真
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
this->close();
}
通过QT助手查看splash screens的 Qt::WA_QuitOnClose属性默认设置为假
Qt::WA_QuitOnClose属性的作用:窗口设置这个属性,接收到closeEvent()就会退出应用程序
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowFlags(Qt::SplashScreen);
this->setAttribute(Qt::WA_QuitOnClose, true);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
this->close();
}
相关源码
link
在函数bool QWidgetPrivate::close_helper(CloseMode mode)
setWindowFlags 的其它常见的参数
Qt::WindowStaysOnTopHint 使用该标志位可以让窗口一直保持在顶层,类似qq登陆的界面。
Qt::FramelessWindowHint 使用该标志位可以让窗口没有标题栏,用户无法移动;放大;缩小窗口。