Qt::SplashScreen 设置后无法正常结束程序进程

Qt::SplashScreen 设置后无法正常结束程序进程

当窗口设置 Qt::SplashScreen 标志位后 close() 无法正常结束程序进程

  1. 设置 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;
}
  1. 因为没有标题栏,所以也没有默认的 关闭 按钮。所以我们使用一个按钮来实现 关闭 的效果。
    在这里插入图片描述
    右键添加槽函数后
void Widget::on_pushButton_clicked()
{
    this->close();
}
  1. 运行程序,此时问题暴露出来了。点击关闭按钮,虽然看不到程序了但是程序的进程还是在运行中。并没有达到想要的效果。
    在这里插入图片描述
  2. 解决办法
#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 使用该标志位可以让窗口没有标题栏,用户无法移动;放大;缩小窗口。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值