将最大化按钮添加到 PyQt4 对话框并保持对话框位于父窗口的中心
我有一个带有大文本框的对话框。我希望用户能够最大化对话框。我已经尝试了以下选项(XML_Editor
是一个 QDialog
实例):
XML_Editor.setWindowFlags(QtCore.Qt.Window 或 QtCore.Qt.WindowMaximizeButtonHint)
-- 显示最大化按钮,但不再将对话框置于父级的中心
XML_Editor.setWindowFlags(QtCore.Qt.Dialog or QtCore.Qt.WindowMaximizeButtonHint 或 QtCore.Qt.CustomizeWindowHint)
-- 没有效果
现在我的问题是:如何实现最大化按钮的显示以及对话框在父级居中弹出?网络上的大多数资源似乎都集中在如何摆脱最大化按钮。有什么想法可以达到相反的效果吗?
主要目标是 Ubuntu 10.04(默认配置),如果它也能在 Windows 和 Mac 上运行,那就太好了。
我很感激任何提示。提前致谢。
I have a dialog with a big textbox. I want the user to be able to maximize the dialog. I already tried the following options (XML_Editor
is a QDialog
instance):
XML_Editor.setWindowFlags(QtCore.Qt.Window or QtCore.Qt.WindowMaximizeButtonHint)
-- shows the Maximize button but doesn't center the dialog w.r.t. the parent anymore
XML_Editor.setWindowFlags(QtCore.Qt.Dialog or QtCore.Qt.WindowMaximizeButtonHint or QtCore.Qt.CustomizeWindowHint)
-- no effect
Now my question is: How do I achieve that the Maximize button is shown and the dialog pops up centered w.r.t. the parent? Most resources on the web seem to focus on how to get rid of the Maximize button. Any ideas how to achieve the opposite?
The main target is Ubuntu 10.04 (default configuration), it would be great if it worked on Windows and Mac, too.
I appreciate any hint. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要平台上的各种窗口管理器的行为都会有所不同,因此很难想出一个 100% 保证在所有情况下都能工作的解决方案。
使用默认windowFlags 在 Linux 上,KDE 会生成一个带有上下文、最大化、最小化和关闭按钮的对话框;但在 Windows XP 中,只有一个上下文和关闭按钮。
对于大多数平台,至少需要设置 WindowSystemMenuHint 和 WindowMaximizeButtonHint 标志才能确保最大化按钮存在。
为了确保对话框保持在父窗口的中心,只需在对话框的构造函数中传递对父窗口的引用即可。
(请注意,组合标志时,您必须使用按位 OR 运算符
|
。在您的示例中,您使用布尔 OR 运算符或
,这将选择第一个非零值,但忽略其他值)。以下示例在 Linux KDE 和 Windows XP 上为我生成一个带有最大化按钮的居中对话框:
The various window managers on the main platforms will all behave somewhat differently, so it will be hard to come up with a solution that is 100% guaranteed to work in all cases.
Using the default windowFlags on Linux KDE produces a dialog with context, maximize, minimize and close buttons; but with Windows XP, there's only a context and close button.
For most platforms, it would appear that at least the
WindowSystemMenuHint
andWindowMaximizeButtonHint
flags need to be set in order to ensure the maximize button is present.To ensure that the dialog stays centered on the parent window, just pass a reference to the parent in the dialog's constructor.
(Note that when combining the flags, you must use the bitwise OR operator
|
. In your examples, you use the boolean OR operatoror
, which would select the first non-zero value, but ignore the others).The following example produces a centered dialog with maximize button for me on both Linux KDE and Windows XP:
尝试获取父窗口的几何形状,然后将对话框移动到其中心,如下所示:
Try to get the geometry of the parent window and then move the dialog to its center, something like: