模态对话框是如何实现的? [英] How modal dialog is implemented?

查看:32
本文介绍了模态对话框是如何实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间我一直想知道模态对话框是如何实现的.

For a long time I have been wondering how modal dialog is implemented.

让我以 Qt 为例.(几乎所有的GUI工具包都有这个机制)

Let me take Qt as an example. (Nearly all GUI toolkit has this mechanism)

在主事件循环中,调用了一个槽,并在该槽中打开了一个模态对话框.在对话框关闭之前,插槽不会将控制权返回给主事件循环.所以我认为主事件循环被阻塞并变得无响应.显然这不是真的,因为当您打开模态对话框时,背景主窗口仍在工作,例如重新绘制其 UI 或继续显示曲线或某些图形.它只是变得不接受任何用户输入.

In the main event loop, a slot is called, and in this slot a modal dialog is opened. Before the dialog is closed, the slot doesn't return control to the main event loop. So I thought that the main event loop is blocked and become unresponsive. Apparently this is not true, since when you open a modal dialog, the background main window is still working, like repainting its UI or keep displaying a curve or some graph. It just becomes not to accept any user input.

我做了一个实验.我没有在插槽中打开模态对话框,而是在那里启动一个新线程,然后等待线程在该插槽中完成.这肯定阻塞了主事件循环.

I did an experiment. I didn't open a modal dialog in the slot, but start a new thread there, and wait for the thread to finish in that slot. This definitely blocked the main event loop.

模态对话框到底是怎么实现的?它如何保持主事件循环畅通,但同时阻塞调用槽?

How modal dialog is implemented after all? How does it keep main event loop unblocked but at the same time blocked the calling slot?

推荐答案

只需要一个事件循环,并且当模态对话框出现时它不会阻塞.不过,我想,不同的工具包可能会以不同的方式处理这个问题.您需要查阅文档才能确定.然而,从概念上讲,这一切都以相同的方式工作.

There is only ever a need for a single event loop, and it does not block when a modal dialog appears. Though, I suppose, different toolkits may handle this differently. You would need to consult the documentation to know for sure. Conceptually, however, it all works in the same way.

每个事件都有一个事件发生的来源.当模态对话框出现时,事件循环会忽略或重定向所有源自对话框之外的事件.它真的没有什么魔力.一般来说,它就像事件循环代码中的 if 语句,表示if (modal_is_shown() and !event_is_in_modal_window()) {ignore_and_wait_for_next_event()}".当然,逻辑有点复杂,但这就是它的要点.

Every event has a source where the event occured. When a modal dialog appears, the event loop either ignores or redirects all events that originate outside of the dialog. There's really no magic to it. Generally speaking, it's like an if statement in the event loop code that says "if (modal_is_shown() and !event_is_in_modal_window()) {ignore_and_wait_for_next_event()}". Of course, the logic is a bit more complicated, but that's the gist of it.

这篇关于模态对话框是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆