Qt::WA_QuitOnClose用法

本文详细探讨了Qt框架中关于窗口关闭导致应用程序退出的行为。默认情况下,当最后一个具有Qt::WA_QuitOnClose属性的窗口关闭时,应用会退出。通过设置QtWidgetsApplication1的Qt::WA_QuitOnClose为false,可以改变这一行为,使得关闭特定窗口时不退出程序。实验展示了不同设置下关闭窗口的顺序如何影响程序退出的逻辑。

Qt官方助手对该标志的解释如下:

Makes Qt quit the application when the last widget with the attribute set has accepted closeEvent(). This behavior can be modified with the QApplication::quitOnLastWindowClosed property. By default this attribute is set for all widgets of type Qt::Window.

大意是:

  当最后一个设置了本属性的窗体部件关闭时(即接受了closeEvent()事件)退出应用程序。这个属性可以被QApplication::quitOnLastWindowClosed函数修改。对所有类型为Qt::Window.的窗体,这个属性默认被设置为true。

如下:

#include "QtWidgetsApplication1.h"
#include "QtTestWidget.h"
QtWidgetsApplication1::QtWidgetsApplication1(QWidget* parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    setWindowTitle("WidgetsApplication1");
   // setAttribute(Qt::WA_QuitOnClose, false);

    m_pTestWidget = new QtTestWidget();
    m_pTestWidget->show();
}

QtWidgetsApplication1::~QtWidgetsApplication1()
{
    
}



其中 m_pTestWidget是QtWidgetsApplication1的成员变量,类型为:QtTestWidget。QtTestWidget.cpp如下:

#include "QtTestWidget.h"

QtTestWidget::QtTestWidget(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	setWindowTitle("TestWidget"); 

   // setAttribute(Qt::WA_QuitOnClose, false);
}

QtTestWidget::~QtTestWidget()
{
}

 

弹出标题为:WidgetsApplication1、TestWidget两个窗体。按Qt助手的解释:这两个窗体的Qt::WA_QuitOnClose默认都为true,所以先关闭其中任何一个窗体,程序不会退出,只有当最后一个窗体关闭时,应用程序才退出。

  当将QtWidgetsApplication1.cpp构造函数中的setAttribute(Qt::WA_QuitOnClose, false);注释去掉,则:

a):关闭WidgetsApplication1窗体,程序不退出关闭TestWidget窗体,程序才退出。因为此时WidgetsApplication1窗体的Qt::WA_QuitOnClose为false,即剔除了该属性;TestWidget窗体的Qt::WA_QuitOnClose依然为true,即TestWidget窗体是包含Qt::WA_QuitOnClose属性的最后一个窗体,当最后一个包含Qt::WA_QuitOnClose属性的窗体退出时,程序退出。

b):先关闭TestWidget窗体,程序退出。道理同a)条目所述。

  当将QtWidgetsApplication1.cpp、QtTestWidget.cpp构造函数中的setAttribute(Qt::WA_QuitOnClose, false);注释都去掉,无论先关闭哪个窗体或两个窗体都关闭,应用程序都不会退出

在PACKZONGFUWU.cpp中 configWindow1 = NULL; configWindow2 = new hanhou; connect(configWindow2, &hanhou::jilu2, this, &PACKZONGFUWU::test2, Qt::UniqueConnection); 在hanhou.cpp中 ui.setupUi(this); hanhoujiance(); this->setWindowTitle(tr("PACK焊后检测传输服务")); this->setAttribute(Qt::WA_QuitOnClose, false); QThread* thread3 = new QThread(); WorkL* work3 = new WorkL(); work3->moveToThread(thread3); QObject::connect(thread3, SIGNAL(started()), work3, SLOT(WorkL1())); QObject::connect(thread3, SIGNAL(finished()), thread3, SLOT(deleteLater())); QObject::connect(work3, SIGNAL(xiaoxiL(QString)), this, SLOT(shibaixiaoxiL(QString))); QObject::connect(work3, SIGNAL(zt2()), this, SLOT(hanhoujiance())); thread3->start(); 在WorkL.cpp中 totalBytes1 = 0; //总大小 bytesReceived1 = 0; //已经接受的数据包大小 fileNameSize1 = 0; //文件名大小 TcpServer5 = new QTcpServer(); TcpSocket5 = new QTcpSocket(); TcpServer5->listen(QHostAddress(ip), 9000);//焊后检测发送图片数据给服务器 connect(TcpServer5, &QTcpServer::newConnection, this, &WorkL::lianjie5); TcpServer6 = new QTcpServer(); TcpSocket6 = new QTcpSocket(); TcpServer6->listen(QHostAddress(ip), 9001);//视觉客户端发消息给服务器,服务器写入数据库 connect(TcpServer6, &QTcpServer::newConnection, this, &WorkL::lianjie6); timer1 = new QTimer; connect(timer1, &QTimer::timeout, this, &WorkL::timer1_timeout, Qt::UniqueConnection); timer1->start(600000);//2分钟 TcpServer7 = new QTcpServer(); TcpSocket7 = new QTcpSocket(); TcpServer7->listen(QHostAddress(ip), 9002);//传输完成服务器写入数据库 connect(TcpServer7, &QTcpServer::newConnection, this, &WorkL::lianjie7); 在bianliang.cpp中 //焊后检测 QTcpServer* TcpServer5 = new QTcpServer; QTcpSocket* TcpSocket5 = new QTcpSocket; QTcpServer* TcpServer6 = new QTcpServer; QTcpSocket* TcpSocket6 = new QTcpSocket; QTcpServer* TcpServer7 = new QTcpServer; QTcpSocket* TcpSocket7 = new QTcpSocket; 为什么客户端一连接9001端口程序就闪退
04-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值