如何允许在 PyQt4 中调整 QMessageBox 的大小 [英] How to allow resizing of QMessageBox in PyQt4

查看:123
本文介绍了如何允许在 PyQt4 中调整 QMessageBox 的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 QMessageBox 中的好功能来选择性地向用户显示详细文本.然而,扩展后的窗口仍然相当小,人们立即尝试调整窗口大小,以便更多细节可见.即使设置了我认为正确的设置后,它也不允许调整大小.

I'm using the nice feature in QMessageBox to optionally show detailed text to the user. However, the window after expansion is still fairly small, and one immediately tries to resize the window so more of the details are visible. Even after setting what I think are the proper settings it won't allow resizing.

这是 PyQt4 代码的相关片段:

Here's the relevant snippet of PyQt4 code:

mb = QMessageBox()
mb.setText("Results written to '%s'" % filename)
mb.setDetailedText(str(myData))
mb.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
mb.setSizeGripEnabled(True)

我是否遗漏了一个步骤和/或这完全可能吗?

Am I missing a step and/or is this at all possible?

推荐答案

这是我会使用的解决方案.这不会使对话框调整大小,但它确实使对话框在详细信息框可见时将自身更改为合理的大小.我毫不客气地从 serge_gubenko 的回答中窃取了一些想法.即使您更愿意实施他的调整大小,我也会在下面提供一些其他改进.

This is the solution I would use. This doesn't make the dialog resizable, but it does make the dialog change itself to a sensible size when the details box is visible. I have unashamedly stolen some ideas from serge_gubenko's answer. Even if you'd rather implement his resizing I humbly offer some other improvements below.

# Safe since everything in the namespace begins with 'Q'
from PyQt4.QtGui import *

class MyMessageBox(QMessageBox):

    # This is a much better way to extend __init__
    def __init__(self, *args, **kwargs):            
        super(MyMessageBox, self).__init__(*args, **kwargs)
        # Anything else you want goes below

    # We only need to extend resizeEvent, not every event.
    def resizeEvent(self, event):

        result = super(MyMessageBox, self).resizeEvent(event)

        details_box = self.findChild(QTextEdit)
        # 'is not' is better style than '!=' for None
        if details_box is not None:
            details_box.setFixedSize(details_box.sizeHint())

        return result

这篇关于如何允许在 PyQt4 中调整 QMessageBox 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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