Qt: 窗口的显示和隐藏_qt释放还是隐藏对话框dialog.accept()-程序员宅基地

技术标签: Qt 笔记  qt  

隐藏窗口

1. hide()

Hides the widget.

This function is equivalent to setVisible(false).

Note: If you are working with QDialog or its subclasses and you invoke the show() function after this function, the dialog will be displayed in its original position.

2. setVisible(false)

Calling setVisible(false) or hide() hides a widget explicitly.

An explicitly hidden widget will never become visible, even if all its ancestors become visible, unless you show it.

3. lower()

Lowers the widget to the bottom of the parent widget’s stack.

After this call the widget will be visually behind (and therefore obscured by) any overlapping sibling widgets.

4. close()

Closes this widget.

Returns true if the widget was closed; otherwise returns false.

First it sends the widget a QCloseEvent. The widget is hidden if it accepts the close event. If it ignores the event, nothing happens. The default implementation of QWidget::closeEvent() accepts the close event.

If the widget has the Qt::WA_DeleteOnClose(widget attribute, which is set and cleared with void QWidget::setAttribute(Qt::WidgetAttribute attribute, bool on = true)) flag, the widget is also deleted. (the difference with hide())

this->setAttribute(Qt::WA_DeleteOnCLose, true);

A close events is delivered to the widget no matter if the widget is visible or not.

The QApplication::lastWindowClosed() signal is emitted 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.

5. hideEvent()

This event handler is called with the given event when Qt receives a window close request for a top-level widget from the window system.

By default, the event is accepted and the widget is closed.

You can reimplement this function to change the way the widget responds to window close requests. For example, you can prevent the window from closing by calling ignore() on all events.

  void MainWindow::closeEvent(QCloseEvent *event)
  {
    
      if (maybeSave()) {
    
          writeSettings();
          event->accept();
      } else {
    
          event->ignore();
      }
  }

 

显示窗口

1. show()

Shows the widget and its child widgets.

This is equivalent to calling showFullScreen(), showMaximized(), or setVisible(true), depending on the platform’s default behavior for the window flags.

2. setVisible(true)

Calling setVisible(true) or show() sets the widget to visible status if all its parent widgets up to the window are visible.

If an ancestor is not visible, the widget won’t become visible until all its ancestors are shown.

If its size or position has changed, Qt guarantees that a widget gets move and resize events just before it is shown.

If the widget has not been resized yet, Qt will adjust the widget’s size to a useful default using adjustSize().

A widget that happens to be obscured by other windows on the screen is considered to be visible.

3. raise()

Raises this widget to the top of the parent widget’s stack.

After this call the widget will be visually in front of any overlapping sibling widgets.

4. exec()

 int QDialog::exec()

Shows the dialog as a modal dialog, blocking until the user closes it.

The function returns a DialogCode result.

QDialog::Accepted 1

QDialog::Rejected 0

If the dialog is application modal, users cannot interact with any other window in the same application until they close the dialog.

If the dialog is window modal, only interaction with the parent window is blocked while the dialog is open.

By default, the dialog is application modal.

用法

当需要一直显示一个窗口时,用 exec(),不同于 show()exec() 显示的只能是 模态窗口,且只要去关闭窗口,操作权会一直在该窗口,开启事件循环,直到关闭该窗口。

关闭窗口常用函数有:
1、accept()

void QDialog::accept()

Hides the modal dialog and sets the result code to Accepted.

该函数只隐藏窗口,并不删除窗口,并设置返回值为 Accepted

2、reject()

void QDialog::reject()

Hides the modal dialog and sets the result code to Rejected.
该函数同样隐藏窗口,并返回值 Rejected

3、done(int r)

 void QDialog::done(int r)

Closes the dialog and sets its result code to r. If this dialog is shown with exec(), done() causes the local event loop to finish, and exec() to return r.

As with QWidget::close(), done() deletes the dialog if the Qt::WA_DeleteOnClose flag is set.
If the dialog is the application’s main widget, the application terminates.
If the dialog is the last window closed, the QApplication::lastWindowClosed() signal is emitted.

该函数 close 窗口,窗口会隐藏,是否删除取决于是否设置 Qt::WA_DeleteOnClose 。且该函数可以自己设置返回值。

这样可以利用返回值做出相应判断,如显示一个需要根据用户选择而决定下一步操作的对话框,如有三个选项:ABC,可以根据不同选择用 done() 函数设置不同返回值,从而进行不同处理。

4、setResult(int i)

void QDialog::setResult(int i)

Sets the modal dialog’s result code to i.

5. showEvent() ?

[virtual protected] void QWidget::showEvent(QShowEvent *event)

This event handler can be reimplemented in a subclass to receive widget show events which are passed in the event parameter.

Non-spontaneous show events are sent to widgets immediately before they are shown.

The spontaneous show events of windows are delivered afterwards.

Note: A widget receives spontaneous show and hide events when its mapping status is changed by the window system, e.g. a spontaneous hide event when the user minimizes the window, and a spontaneous show event when the window is restored again.

After receiving a spontaneous hide event, a widget is still considered visible in the sense of isVisible(). (why???)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Lee567/article/details/106162978

智能推荐

ip-guard V4加密原理更新成驱动层加密_ip-guard原理-程序员宅基地

文章浏览阅读1.5k次。V3应用层加密应用层透明加密技术俗称钩子透明加密技术。这种技术就是将两种技术(应用层API和Hook)组合而成的。通过windows的钩子技术,监控应用程序对文件的打开和保存,当打开文件时,先将密文转换后再让程序读入内存,保证程序读到的是明文,而在保存时,又将内存中的明文加密后再写入到磁盘中。应用层透明加密(钩子透明加密)技术与应用程序密切相关,它是通过监控应用程序的启动而启动的。V4驱动层加密基于windows的文件系统(过滤)驱动(IFS)技术,工作在windows的内核层。当应用程_ip-guard原理

Java集合框架源码分析(四)——LinkedHashMap_linkedhashmap 循环-程序员宅基地

文章浏览阅读649次。LinkedHashMap简介LinkedHashMap是HashMap的子类,与HashMap有着同样的存储结构,但它加入了一个双向链表的头结点,将所有put到LinkedHashmap的节点一一串成了一个双向循环链表,因此它保留了节点插入的顺序,可以使节点的输出顺序与输入顺序相同。LinkedHashMap可以用来实现LRU算法(这会在下面的源码中进行分析)。LinkedHashMap同样是非线_linkedhashmap 循环

面向对象编程_数据操作类(暂缺删改功能)_面向对象54_添加功能--接收数据保存-程序员宅基地

文章浏览阅读216次。1、MySQLDB.class.php注意:数据库操作类用的是单例模式。<?phpclass MySQLDB{ //数据库连接信息 private $dbConfig=array( 'host'=>'localhost', 'port'=>'3306', 'user'=>'', 'pwd'=>'', 'charset'=>'utf8', 'dbna_面向对象54_添加功能--接收数据保存

Qt知识点总结_qt知识分享-程序员宅基地

文章浏览阅读1.9k次,点赞5次,收藏37次。QT提供了一些机制来保证线程安全,如互斥量(QMutex)、信号量(QSemaphore)和读写锁(QReadWriteLock)。这些机制可以在多线程环境下实现数据的同步访问和线程间的协调。_qt知识分享

spark原理简介_spark技术原理-程序员宅基地

文章浏览阅读773次。spark简介以及原理spark简介 spark是基于内存的分布式处理框架,它把要执行的作业拆分成多个任务,然后将任务分发到多个CPU进行处理,处理结果的中间数据存储在内存中,减少了数据处理过程中对硬盘的I/O操作,大大提升了处理效率。spark和MapReduce对比 spark相对于mr,性能上提高了100倍。 &_spark技术原理

大数据Hadoop入门_hadoop只用一个账户能启动-程序员宅基地

文章浏览阅读4.6k次,点赞4次,收藏34次。先安装好vmware并且创建一台虚拟机,IP和主机名配置1.点击vmware的“编辑” =>虚拟网络编辑器(N)...2.点击“VMnet8"后点击”更改设置“3.而后再次点击VMnet8,修改子网IP地址为:192.168.10.0(IP可以任意取值只要不为192.168.1.0即可)4.修改完成后,点击NAT设置,将网关的地址修改与子网IP在同一网段。这里网关IP设置为192.168.10.2 ;随后点击确定=>确定5.配置主机的IP,网..._hadoop只用一个账户能启动

随便推点

SINS/GNSS组合导航仿真应用详细版(基于PSINS工具箱 )_psins中gps时间设置-程序员宅基地

文章浏览阅读5.5k次,点赞11次,收藏87次。文章目录轨迹仿真生成轨迹数据绘制仿真数据惯导仿真纯惯导仿真SINS/GPS组合导航总结12345轨迹仿真生成轨迹数据首先,打开demos\test_trj.m文件,运行仿真生成轨迹。运行后,将轨迹数据存文件并对AVP参数和IMU数据作图。% Trajectory generation for later simulation use.% See also test_SINS, test_SINS_GPS_153, test_DR.% Copyright(c) 2009-2014, by Go_psins中gps时间设置

最火照片墙前端纯HTML(只需添加照片)_照片墙html-程序员宅基地

文章浏览阅读1.4w次,点赞57次,收藏245次。最火照片墙_照片墙html

Hibernate学习笔记(五)—— Hibernate查询方式-程序员宅基地

文章浏览阅读187次。一、对象图导航查询  对象图导航查询方式是根据已经加载的对象,导航到他的关联对象。它利用类与类之间的关系来查询对象。比如要查找一个联系人对应的客户,就可以由联系人对象自动导航找到联系人所属的客户对象。当然,前提是必须在对象关系映射文件上配置了多对一的关系。其检索方式如下所示:LinkMan linkMan = session.get(LinkMan.class, 1l);..._hibernate having

C# 三种用控制台从键盘获取输入的方法_c#获取键盘输入-程序员宅基地

文章浏览阅读1.6w次,点赞9次,收藏39次。C# 三种用控制台从键盘获取输入的方法//1 //Console.Read();//作用:得到键盘输入的ASC码 默认int类型 (读取一行,直到按下回车)int i = Console.Read()-48; //直接使用//2//Console.ReadKey().KeyChar /*作用:得到键盘输入的一个字符,默认char类型 可以赋给int double char(立即反应,没有缓冲区)* char类型不进行强制类型转换的话,不能直接减去一个数字。* 但是char类型,在Cons_c#获取键盘输入

基于Vue2的图书销售网站(HTML+CSS+JS)_vue期末作业购物网站-程序员宅基地

文章浏览阅读1.2w次,点赞55次,收藏252次。vue框架搭建的图书销售网站,功能包括登录注册,分类列表,书籍查询,书籍详情,添加购物车。_vue期末作业购物网站

spring-搭建web项目_spring搭建web项目-程序员宅基地

文章浏览阅读3.7k次,点赞2次,收藏6次。spring-搭建web项目1.创建maven,web项目2.加入依赖3.拷贝ch07-spring-mybatis中的代码和配置文件4.创建一个jsp发起请求,有参数id,name,email,age。5.创建Servlet,接收请求参数,调用Service,调用dao完成注册6.创建一个jsp作为显示结果的页面1.之前做的是javase项目有main方法的,执行代码是执行main方法的,在main里面创建的容器对象。ApplicationContext ctx = new ClassPathXml_spring搭建web项目