问题描述
我不明白为 QMessageBox
设置父级的好处是什么,例如在以下代码中:
void mainWindow :: showMessage(QString msg){
QMesageBox :: information(this,title,msg); //'this'is parent
}
有人可以帮助我吗?
可能是几件事。首先 QMessageBox
继承自 QDialog
。由于 QDialog
有一个父的概念, QMessageBox
也应该为一致性。
具体来说,文档说:
至少,一个新的对话框通常显示在其父对象的中心。
还有更多!
根据文档,它可以实际上实现功能。例如:
此外,还有一个窗口模态和应用模式,其中前者仅防止父窗口中的输入,后者防止整个应用的输入。这显然需要知道父对象的概念。
最后,对于某些 static
函数,如 :: about(...)
,它寻找一个图标的第一个地方是 parent-> icon()
。
所以,如果你想获得不错的平台特定的行为,并让你的代码跨平台,你最好不要传递一个理智的父。 >
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 ?
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:
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 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.
这篇关于设置QMessageBox的父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!