设置QMessageBox的父级 [英] Setting parent for a QMessageBox

查看:1732
本文介绍了设置QMessageBox的父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为 QMessageBox 设置父级的好处是什么,例如在以下代码中:

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

有人可以帮助我吗?

解决方案

可能是几件事。首先 QMessageBox 继承自 QDialog 。由于 QDialog 有一个父的概念, QMessageBox 也应该为一致性。



具体来说,文档说:


parent传递给QDialog构造函数。


至少,一个新的对话框通常显示在其父对象的中心。



还有更多!



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


在Mac OS X上,如果您希望消息框显示为
的Qt :: Sheet ,将消息框的窗口模式设置为Qt :: WindowModal
或使用open()。


此外,还有一个窗口模态和应用模式,其中前者仅防止父窗口中的输入,后者防止整个应用的输入。这显然需要知道父对象的概念。



最后,对于某些 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:

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.

这篇关于设置QMessageBox的父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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