bool QWidget::close()的小细节

1 关闭widget。如果widget被关闭返回true,否则返回false。
它首先向widget发送QCloseEvent事件。如果窗体接收到close事件就隐藏。如果它忽略了该事件,就什么都不会发生。QWidget::closeEvent()的默认实现接收close事件。
如果widget有Qt::WA_DeleteOnClose标志,则widget也会被删除。无论widget是否可见close事件都会投递到widget。
当最后一个带有Qt::WA_QuitOnClose属性的可见主窗口被关闭时,会发送QApplication::lastWindowClosed()信号。该属性默认所有widget都有,除了splash screens, tool windows, and popup menus这些临时窗体。

void QWidget::hide() [slot]
Hides the widget. This function is equivalent to setVisible(false).
隐藏widget。该函数等同于 setVisible(false)。

那么close和hide区别就很明显了:
hide只是隐藏窗体。不会发送任何信号。
close一般也是隐藏窗口。但是它会发送QCloseEvent事件。你可以重写void QWidget::closeEvent(QCloseEvent * event) [virtual protected],可以隐藏widget或者不隐藏。Qt::WA_DeleteOnClose标志还会影响窗体在内存中的状态,如果设置了该标志,窗体就会被删除,而hide则不会。最后主窗体的close会导致整个程序的退出,而hide明显不会。

PS:再对一个窗体调用close函数后,如果再调用show(),这个窗体又会被显示出来。

2 关闭窗口与终止程序
一个(应用)程序通常拥有多个窗口,关闭(或删除)一个窗口,并不一定会使程序终止,Qt中关闭窗口使用QWidget::close()槽函数,终止程序使用的是QCoreAppli

/home/jiufeng/Qt_test/MyWindow.cpp:10: error: no matching member function for call to 'connect' /home/jiufeng/Qt_test/MyWindow.cpp:10:5: error: no matching member function for call to 'connect' 10 | connect(this,&QPushButton::clicked,this,&MyWindow::close); | ^~~~~~~ /home/jiufeng/Qt/6.9.3/gcc_64/include/QtCore/qobject.h:209:36: note: candidate function not viable: no known conversion from 'void (QAbstractButton::*)(bool)' to 'const char *' for 2nd argument 209 | static QMetaObject::Connection connect(const QObject *sender, const char *signal, | ^ ~~~~~~~~~~~~~~~~~~ /home/jiufeng/Qt/6.9.3/gcc_64/include/QtCore/qobject.h:212:36: note: candidate function not viable: no known conversion from 'void (QAbstractButton::*)(bool)' to 'const QMetaMethod' for 2nd argument 212 | static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal, | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~ /home/jiufeng/Qt/6.9.3/gcc_64/include/QtCore/qobject.h:230:9: note: candidate function [with Func1 = void (QAbstractButton::*)(bool), Func2 = bool (QWidget::*)()] not viable: no known conversion from 'MyWindow *' to 'const typename QtPrivate::FunctionPointer<void (QAbstractButton::*)(bool)>::Object *' (aka 'const QAbstractButton *') for 1st argument 230 | connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/jiufeng/Qt/6.9.3/gcc_64/include/QtCore/qobject.h:405:41: note: candidate function not viable: no known conversion from 'void (QAbstractButton::*)(bool)' to 'const char *' for 2nd argument 405 | inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal, | ^ ~~~~~~~~~~~~~~
最新发布
11-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值