How to allow resizing of QMessageBox in PyQt4

13,013

Solution 1

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

Solution 2

if you're looking to make a resizable message box, pls, check if code below would work for you:

class MyMessageBox(QtGui.QMessageBox):
    def __init__(self):
        QtGui.QMessageBox.__init__(self)
        self.setSizeGripEnabled(True)

    def event(self, e):
        result = QtGui.QMessageBox.event(self, e)

        self.setMinimumHeight(0)
        self.setMaximumHeight(16777215)
        self.setMinimumWidth(0)
        self.setMaximumWidth(16777215)
        self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)

        textEdit = self.findChild(QtGui.QTextEdit)
        if textEdit != None :
            textEdit.setMinimumHeight(0)
            textEdit.setMaximumHeight(16777215)
            textEdit.setMinimumWidth(0)
            textEdit.setMaximumWidth(16777215)
            textEdit.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)

        return result

here's how messagebox is called:

mb = MyMessageBox()
mb.setText("Results written to '%s'" % 'some_file_name')
mb.setDetailedText('some text')
mb.exec_()

solution is taken from here

hope this helps, regards

Share:
13,013
metasim
Author by

metasim

I design and develop software for use in data science applications, primarily in Scala. I've been programming for the JVM since 1996, primarily in the desktop application development for science and engineering solutions. I'm currently focusing on text analytics using graph database techniques. In my free time I build and fly RC airplanes and multi-rotors, and contribute to a few open source Scala projects.

Updated on July 25, 2022

Comments

  • metasim
    metasim almost 2 years

    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.

    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?