设置 QMessageBox 的父级
我无法理解为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
大概有几件事。首先,
QMessageBox
继承自QDialog
。由于QDialog
具有父级的概念,因此QMessageBox
也应该具有一致性。具体来说,文档说:
至少,新对话框通常显示在其父对话框顶部的中心位置。
然而,还有更多!
根据文档,它可以影响实际功能。例如:
此外,还有“窗口模态”和“应用程序模态”的概念,前者仅阻止父窗口中的输入,后者阻止整个应用程序的输入。这显然需要了解父母的概念。
最后,对于某些
static
函数,例如::about(...)
,它查找要使用的图标的第一个位置是parent->icon ()
。因此,如果您想获得良好的特定于平台的行为并使您的代码跨平台,那么最好将一个健全的父级传递给它。
Probably a few things. First of all
QMessageBox
inherits fromQDialog
. SinceQDialog
has a concept of a parent,QMessageBox
should too for consistency.Specifically, the documentation says:
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:
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 isparent->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.
对话框的父子层次结构定义了各种平台中的窗口堆叠行为。如果将对话框 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.
其他答案可能更好,但我自己的小原因是它将消息框放在父级的中心而不是屏幕的中心......
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...
不要忘记提及 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 ...如果您不使用静态函数,而是实际创建 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.