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

智能推荐

jQuery-Ajax(详解)_jquery.ajax-程序员宅基地

文章浏览阅读2.7w次,点赞77次,收藏419次。安装Web环境Ajax方法需要与Web服务器端进行交互,需要有环境才可正常使用,安装环境的工具包有很多,可以选择自行下载。jQuery中的Ajax在jQuery中,$.Ajax()方法属于最底层的方法,第2层是load(),$.get(),和$.post(),第3层是$.getScript()和$.getJSON()方法。一、 load( ) 方法结构load( url , [data] , [callback] )参数解释捕获.PNG1.1 应用1.1.1首先构建一_jquery.ajax

abaqus切削为什么没有切屑_基于ABAQUS的高速切削切屑形成过程的有限元模拟-程序员宅基地

文章浏览阅读809次。【实例简介】基于有限元分析软件ABAQUS的Johnson-Cook材料模型以及断裂准则模拟高速切削淬硬钢锯齿状切屑形态,并讨论刀具前角和锯齿状切屑形态对切削力的影响。研究表明仿真结果和试验结果是一致的,文中介绍的有限元模拟方法可以准确地模拟并预测高速切削淬硬钢时的切屑形成过程。刀4有限元模拟及试验结果将有限元模拟仿真预测的切屑形态与试验结果进行比较,如图2、图3、图4所示。预测的切屑形态结果以积..._abaqus切屑

python判断素数的函数_python基础——filter函数-程序员宅基地

文章浏览阅读1.2k次。python基础——filter函数Python内建的filter()函数用于过滤序列。和map()类似,filter()也接收一个函数和一个序列。和map()不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。例如,在一个list中,删掉偶数,只保留奇数,可以这么写:#在一个list中,删掉偶数,只保留奇数def is_odd(..._并发编程判断大素数def main(): if __name__ == '__main__': primes=list(map(int

Zookeeper-api基础教程_zookeeper入门初体验头歌答案-程序员宅基地

文章浏览阅读1.2k次,点赞2次,收藏14次。第一关、创建会话与关闭【题目来自头歌enducoder平台,有帮助的话,不忘点个赞、关注哟!】import java.io.IOException;import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper;public class test1 { // 初始化 ZooKeeper 实例 privat_zookeeper入门初体验头歌答案

你应该知道的10种软件工具_wzk工具箱软件-程序员宅基地

文章浏览阅读999次。除非你是设计小型模拟电子电路,不然这年头离开了计算机的帮助,在嵌入式系统设计中很难做成什么事。我觉得我应该分享一个能帮助我完成工作的软件工具列表。它们大多数都是免费的或者比较便宜的。它们大多数也和软件一起工作。如果你向来不需要设计,阅读或者编辑任何软件,那么你属于读了这篇文章不会从中受益的那一小部分人。免责声明:“最佳”软件工具通常是一种主张。你可能不同意我的主张,那就取其精华吧。_wzk工具箱软件

Asp.net Ajax ASP.NET 局部更新PostBack的客户端调用过程 _asp.net postback viewstate ajax-程序员宅基地

文章浏览阅读2k次。Sys.WebForms.PageRequestManager._initialize(ScriptManager1, document.getElementById(form1));Sys.WebForms.PageRequestManager.getInstance()._updateControls([tUpdatePanel1], [], [], 90); 看一下Micro_asp.net postback viewstate ajax

随便推点

夜神模拟器浏览器下载安装burp证书,der证书安装不上 的解决办法。_windows 夜神证书 安装不了-程序员宅基地

文章浏览阅读343次。大家不要慌,咱们进入设置,点击安全,然后看图2。点击下载证书,发现双击打不开。_windows 夜神证书 安装不了

电动汽车常规充电、快速充电、更换电池充电负荷蒙特卡洛法模拟(Matlab代码实现)-程序员宅基地

文章浏览阅读309次,点赞7次,收藏10次。运用蒙特卡洛法模拟电动汽车的充电方式,包括常规充电、快速充电以及更换电池充电曲线,并研究这些方式对日负荷曲线的影响。通过模拟这些充电方式,我们可以深入了解电动汽车在不同充电模式下对电网的影响,从而为未来电动汽车的普及和充电基础设施的规划提供重要参考。通过这样的蒙特卡洛法模拟研究,您可以获得关于不同类型电动汽车充电负荷的信息,帮助优化充电桩的布局和安排,改善充电服务和系统性能。可以计算充电桩的利用率、充电时长的分布、平均等待时间等指标,评估不同类型电动汽车充电负荷的特征和影响因素。行百里者,半于九十。

面向联邦学习的知识图谱系统设计与构建(开题报告)_联邦式知识图谱-程序员宅基地

文章浏览阅读536次,点赞19次,收藏13次。总体而言,面向联邦学习的知识图谱系统设计与构建不仅有望解决当前联邦学习中的一系列挑战,同时也为构建更智能、安全和高效的分布式智能系统提供了新的思路和工程实践。技术方案的选择应考虑如何通过知识图谱系统的设计和优化方法,提高联邦学习模型的训练效率。4.在实验阶段,通过验证系统性能,实际应用于联邦学习场景,为实际应用提供了具体的解决方案,并在实验结果中展现了创新性和可行性。2.提出的模型训练优化方法通过知识图谱系统的应用,为联邦学习中的模型训练问题提供了新的解决思路,具有一定的创新性。_联邦式知识图谱

git 提交代码失败 Make sure no other git process is running and remove the file manually to continue.-程序员宅基地

文章浏览阅读4k次,点赞5次,收藏4次。在提交的代码的时候突然失败了,报错的信息是这样的$ git commit -afatal: Unable to create 'e:/git/Android/XXXXXX/.git/index.lock': File exists.If no other git process is currently running, this probably means agit process c..._git process is running and remove the file manually to continue

Revit导出为OBJ_revit转obj模型-程序员宅基地

文章浏览阅读1k次,点赞18次,收藏23次。3D模型在线转换是一个可以进行3D模型格式转换的在线工具,支持多种3D模型格式进行在线预览和互相转换,并提供Revit、MicroStation、Blender等设计软件插件,实现设计模型在线预览与格式转换。_revit转obj模型

strtotime的结合-1 month, +1 month, next month总结_strtotime('next month-程序员宅基地

文章浏览阅读399次。经常会有人被strtotime结合-1 month, +1 month, next month的时候搞得很困惑, 然后就会觉得这个函数有点不那么靠谱, 动不动就出问题. 用的时候就会很慌...这不, 刚刚就有人在微博上又问我:鸟哥,今天是2018-07-31 执行代码:date("Y-m-d",strtotime("-1 month"))怎么输出是2018-07-01?好的吧, 虽然这个问题看起来很迷惑, 但从内部逻辑上来说呢, 其实是"对"的:我们来模拟下date内部的对于._strtotime('next month