qt 关闭一个页面,结束应用程序运行,关闭其他窗口

本文介绍如何在Qt应用中实现关闭主窗口时自动关闭所有打开的窗口并退出程序。主要通过设置QWidget属性和连接QApplication的信号与槽函数来完成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


在一个PC端的应用中,经常会出现关闭主窗体,进而需要把所有打开的窗体都随着关闭,结束程序的情况,那么具体怎么实现呢?

1、在main函数中将QApplication::lastWindowClosed()信号和QApplication::quit()槽函数相关联:

      a.connect(&a,SIGNAL(lastWindowClosed()),&a,SLOT(quit()));

2、将主窗口的属性设置为QWidget::setAttribute(WA_QuitOnClose,true);其他窗口该属性设置为false:

      注意:在初始化窗口时就要设置,不是在窗口的构造函数里设置

    Princekin w;
    w.setWindowFlags(Qt::WindowCloseButtonHint|Qt::WindowMinimizeButtonHint|Qt::FramelessWindowHint);
    w.setAttribute(Qt::WA_QuitOnClose,true);
    w.show();

具体可参考qt助手中的解释:

void QApplication::lastWindowClosed () [signal]

This signal is emitted from QApplication::exec() when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed.

By default,

  • this attribute is set for all widgets except transient windows such as splash screens, tool windows, and popup menus
  • QApplication implicitly quits when this signal is emitted.

This feature can be turned off by setting quitOnLastWindowClosed to false.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值