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

智能推荐

vue通过v-if实现tab页切换_v-if在tab-程序员宅基地

文章浏览阅读1.9k次,点赞4次,收藏6次。<template> <div class="con"> <el-card class="box-card"> <!-- tab按钮 --> <span class="tabs" :class="{to_active:isShow === 1}" @click="handleTab(1)">tab1</span> <span class="tabs" :class="{to_ac_v-if在tab

直接数字下变频(1):低通滤波法实现_低通滤波器下变频-程序员宅基地

文章浏览阅读6.3k次,点赞3次,收藏87次。实信号的频谱包括正负频谱两部分,正负频谱共轭对称,每一部分都含有信号的全部信息,如果只对其进行单路相位检波的话,正负频谱会在零频附近发生频谱混叠;若想不发生频谱混叠,需要对实信号进行正交双路相位检波,一路为同相支路用来获得同相分量,一路为正交支路用来获得正交分量,它们可以构成复数信号,这样负频率部分在零频附近相互抵消为0,正频率部分在零频附近相互叠加变为原来的2倍,没有发生频谱混..._低通滤波器下变频

RabbitMQ消息队列生产者和消费者_rabbitmq 消费组-程序员宅基地

文章浏览阅读9.3k次。技术:Spring4.3.5+RabbitMQ3.5.1运行环境:Spring4.3.5+RabbitMQ服务+JDK7 概述生产者生产数据至 RabbitMQ 队列,消费者消费 RabbitMQ 队列里的数据。详细一、准备工作1、安装 RabbitMQ 服务和 RabbitMQ Management。RabbitMQ在win_rabbitmq 消费组

Linuxcentos7安装openresty附带启动脚本_centos 安装openresty1.19.3.1-程序员宅基地

文章浏览阅读787次。OpenResty是一个魔改版NGINX,相比原NGINX,有更多的功能,并且极为适合开发人员利用lua语言快速开发。OpenResty的下载十分方便,下载地址为:OpenResty - 中文官方站 ,选择一个适合的版本下载即可。我这里下载的openresty-1.19.3.1.tar.gz编译安装命令为:编译环境安装正式编译:解压上述文件openresty-1.19.3.1.tar.gz,cd到解压目录,执行安装命令:编译完成后,openresty将会安装在/usr/local/openre_centos 安装openresty1.19.3.1

nvm安装nodejs 运行use命令时报错exit status 1: ��û���㹻��Ȩ��ִ�д˲�����...-程序员宅基地

文章浏览阅读482次。这是因为权限不够,将任务管理器使用管理员身份打开即可:_nvm exit status 1:

Vue+Element UI中Select下拉框搜索使用_element ui的下拉框搜索如何实现-程序员宅基地

文章浏览阅读4k次,点赞2次,收藏14次。根据官网总结:<el-select v-model="form.formVal" clearable filterable remote @change="changeVal($event)" :remote-method="getFormList" placeholder="请选择表单"> <el-option v-for="item in formList" :key="item.id" :label="item.formName" .._element ui的下拉框搜索如何实现

随便推点

js复制内容到剪贴板实现复制粘贴功能_js 复制粘贴功能实现-程序员宅基地

文章浏览阅读307次。js复制内容到剪贴板实现复制粘贴功能_js 复制粘贴功能实现

FAT32文件系统介绍-程序员宅基地

文章浏览阅读1.7w次,点赞23次,收藏191次。FAT32文件系统(一)为什么要有文件系统(二)FAT32文件系统组成(三)分步介绍各部分(1) 首先介绍一下MBR(2)DBR介绍(3)FAT表介绍(4) 数据区(一)为什么要有文件系统文件系统是操作系统用于明确存储设备或分区上的文件的方法和数据结构,即在存储设备上组织文件的方法。如果没有文件系统,那我们想找到在硬盘上保存的数据会是一件很麻烦的事情,文件系统的作用是,它负责为用户建立文件、读出、修改,控制文件的读取,我们常说的格式化硬盘就是写入文件系统。文件系统的种类有很多,而且有各自的特点,本文主要_fat32

adb 命令启动、关闭 app_adb 关闭当前activity-程序员宅基地

文章浏览阅读1.9w次,点赞2次,收藏5次。今年我的目标是【个体崛起】,年初写下自己的目标,希望年终会有收获! adb 命令敲黑板的知识点启动app,在终端输入:#adb shell am start -n 包名/启动的activity关闭app,在终端输入#adb shell am force-stop 包名..._adb 关闭当前activity

JavaWeb(入门之环境的搭建)-程序员宅基地

文章浏览阅读368次。01-WEB环境搭建 一、JavaWEB概述 常用软件体系结构 C/S C/S结构即客户端/服务器(Client/Server),例如QQ; 需要编写服务器端程序,以及客户端程序,例如我们安装的就是QQ的客户端程序; 缺点:软件更新时需要同时更新客户端和服务器端两端,比较麻烦; 优点:安全性比较好。 ..._javaweb

爱奇艺轮播图滚动_爱奇艺轮播图代码-程序员宅基地

文章浏览阅读503次。实例:实例代码:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>视频标签栏切换</title> <style> * {_爱奇艺轮播图代码

完美解决JS点击一次却触发了两次的解决办法_javascrip点击事件执行两次 js-程序员宅基地

文章浏览阅读777次,点赞8次,收藏8次。【代码】完美解决JS点击一次却触发了两次的解决办法。_javascrip点击事件执行两次 js