设置 QMessageBox 的父级

发布于 2024-11-25 15:22:06 字数 214 浏览 1 评论 0原文

我无法理解为 QMessageBox 设置父级有什么好处,例如在以下代码中:

void mainWindow::showMessage(QString msg) {
  QMesageBox::information(this, "title", msg); //'this' is parent
}

有人可以帮助我吗?

i can't understand what's the benefit of setting parent for a QMessageBox, for instance in the following code:

void mainWindow::showMessage(QString msg) {
  QMesageBox::information(this, "title", msg); //'this' is parent
}

can somebody help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别念他 2024-12-02 15:22:06

大概有几件事。首先,QMessageBox继承自QDialog。由于QDialog具有父级的概念,因此QMessageBox也应该具有一致性。

具体来说,文档说:

parent 被传递给 QDialog 构造函数。

至少,新对话框通常显示在其父对话框顶部的中心位置。

然而,还有更多!

根据文档,它可以影响实际功能。例如:

在 Mac OS X 上,如果您希望消息框显示为 Qt::Sheet
父级,将消息框的窗口模式设置为 Qt::WindowModal
或使用 open()。否则,消息框将是一个标准对话框。

此外,还有“窗口模态”和“应用程序模态”的概念,前者仅阻止父窗口中的输入,后者阻止整个应用程序的输入。这显然需要了解父母的概念。

最后,对于某些static函数,例如::about(...),它查找要使用的图标的第一个位置是parent->icon ()

因此,如果您想获得良好的特定于平台的行为并使您的代码跨平台,那么最好将一个健全的父级传递给它。

Probably a few things. First of all QMessageBox inherits from QDialog. Since QDialog has a concept of a parent, QMessageBox should too for consistency.

Specifically, the documentation says:

parent is passed to the QDialog constructor.

At the very least, a new dialog is often displayed centered in top of its parent.

However, there is more!

According to the documentation it can effect actually functionality. For example:

On Mac OS X, if you want your message box to appear as a Qt::Sheet of
its parent, set the message box's window modality to Qt::WindowModal
or use open(). Otherwise, the message box will be a standard dialog.

Also, there is a concept of both "Window Modality" and "Application Modality", where the former only prevents input in the parent window, the latter prevents input for the whole application. This obviously requires the concept of a parent to be known.

Finally, for certain static functions such as ::about(...), the first place it looks for an icon to use is parent->icon().

So, if you want to get nice platform specific behavior and have your code be cross platform, you are better off passing a sane parent to it.

dawn曙光 2024-12-02 15:22:06

对话框的父子层次结构定义了各种平台中的窗口堆叠行为。如果将对话框 P 作为对话框 C 的父级传递,则 C 将在所有(桌面)平台上显示在 P 上方。如果传递 0,窗口堆叠将会有所不同,并且通常不会按预期运行。我在 OS X 上见过的最糟糕的此类问题,其中一些消息框出现在主窗口后面,由于消息框是模态的,因此被禁用,没有任何方法可以到达消息框(既没有快捷方式也没有通过鼠标移动窗口)帮助)。
简而言之,我的建议是:总是通过明智的父母。

The parent-child hierarchy of dialogs defines the window stacking behavior in the various platforms. If you pass dialog P as parent of dialog C, C will appear above P on all (desktop) platforms. If you pass 0, the window stacking will differ and usually not behave as wanted. The worst such issues I've seen on OS X, where some message boxes showed up behind the main window, which was disabled as the message boxes being modal, without any way to get to the message box (neither shortcuts nor moving windows via mouse helped).
In short, my suggestion: always pass a sensible parent.

究竟谁懂我的在乎 2024-12-02 15:22:06

其他答案可能更好,但我自己的小原因是它将消息框放在父级的中心而不是屏幕的中心......

The other answers are probably better, but my own little reason is that it puts the message box at the centre of the parent instead of the centre of the screen...

无人问我粥可暖 2024-12-02 15:22:06

不要忘记提及 QMessageBox 将继承其父级的调色板和样式表。相信我,当您使用自定义的复杂样式表时,您不希望消息弹出,就像它们不属于您的应用程序一样......

Don't forget to mention that QMessageBox will inherit of the palette and the style-sheets of its parent. Believe me when you use custom complex style-sheets you don't want you message to pop like they doesn't belong to your application ...

草莓味的萝莉 2024-12-02 15:22:06

如果您不使用静态函数,而是实际创建 QMessageBox 的实例,那么它对于内存管理也很有用。当父实例被删除时,您的实例也将被删除。

It is also useful for memory management if you don't use static functions, but actually create an instance of QMessageBox. When the parent is deleted your instance will be deleted too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文