pyside Toggle resize capability

189 Views Asked by At

I have a qdialog with a button that when is pressed will show a lineText.

This work fine but I wold like that the qdialog is sizeable when the text edit is shown otherwise not

this is the code I use to toggle the visibility of the lineEdit

class AdvancedErrorPopUP(QtWidgets.QDialog):
    def __init__(self, parent, message="Error !!", messageBody="", mess_type='warning'):
        QtWidgets.QDialog.__init__(self)
        self.mainLayout = QtWidgets.QVBoxLayout(self)
        hlay = QtWidgets.QHBoxLayout()
        messageShortError = QtWidgets.QLabel()
        hlay.addWidget(messageShortError)
        more_button = QtWidgets.QPushButton('More')
        more_button.clicked.connect(self.showMore)
        self.lineEdit = QtWidgets.QTextEdit(self)
        self.lineEdit.setHidden(True)
        self.lineEdit.setMinimumHeight(500)
        self.lineEdit.setMinimumWidth(650)
        if len(messageBody) > 60 or len(message) > 60:
            hlay.addWidget(more_button)
            hlay.addItem(QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) )
            more_button.setStyleSheet('background-color:white;')
            messageShortError.setText(message[:60])
            messageBody = message + messageBody
        else:
            messageShortError.setText(message)
            
        self.lineEdit.setHtml(messageBody)
        
        closeButton = QtWidgets.QPushButton("Close")
        closeButton.clicked.connect(self.close)
        self.mainLayout.addLayout(hlay)
        self.mainLayout.addWidget(self.lineEdit)
        self.mainLayout.addWidget(closeButton)
        self.setLayout(self.mainLayout)
        self.resize(600, 400)
        color = 'white'
        mess_type = mess_type.upper()
        if mess_type == 'ERROR':
            color = '#f44336'
        elif mess_type == 'WARNING':
            color = '#ffb600'
        elif mess_type == 'INFO':
            color = '#5bd3ff'
        self.setWindowTitle("%s!!" % (mess_type.capitalize()))
        self.setStyleSheet('background-color:%r;' % (color))
        self.lineEdit.setStyleSheet('background-color:white;')
        closeButton.setStyleSheet('background-color:white;')
        messageShortError.setStyleSheet('font-weight: bold;')
        self.layout().setSizeConstraint(QtWidgets.QLayout.SetFixedSize);
        QtCore.QTimer.singleShot(0, self.resizeMe)

    def showMore(self):
        lineTextHidden = not self.lineEdit.isHidden()
        self.lineEdit.setHidden(lineTextHidden)
        if lineTextHidden:
            self.layout().setSizeConstraint(QtWidgets.QLayout.SetFixedSize);
        else: # we are maximaizing
            self.layout().setSizeConstraint(QtWidgets.QLayout.SetNoConstraint);
        QtCore.QTimer.singleShot(0, self.resizeMe)

    def resizeMe(self):
        self.resize(self.minimumSizeHint())

as in example I use self.layout().setSizeConstraint fro changing the value but it dose not affect the resize behaviour

2

There are 2 best solutions below

1
musicamante On

The result of setting the size constraint is that the top level window could have it's minimum and/or maximum sizes overridden.

Consider that setSizeConstraint is normally not used in normal situations (it's generally used for custom layout subclasses) nor it's generally called again once the layout has been activated, and also that setting it to SetNoConstraint will not reset the above min/max sizes if those values have been explicitly set or implicitly set by a previous setSizeConstraint call.

You also don't need to call resize based on the hint, if the size constraint and min/max sizes have been properly (re)set. The trick is to properly reset the maximum size whenever you want to allow resizing, and that's by using the maximum possible widget size returned by QtWidgets.QWIDGETSIZE_MAX (which is 16777215, 2^24).

class AdvancedErrorPopUP(QtWidgets.QDialog):
    def __init__(self, parent, message="Error !!", messageBody="", mess_type='warning'):
        # ...
        self.layout().setSizeConstraint(QtWidgets.QLayout.SetFixedSize)

    def showMore(self):
        lineTextVisible = self.lineEdit.isHidden()
        self.lineEdit.setVisible(lineTextVisible)
        if lineTextVisible:
            self.layout().setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
            self.setMaximumSize(QtWidgets.QWIDGETSIZE_MAX, QtWidgets.QWIDGETSIZE_MAX)
        else:
            self.layout().setSizeConstraint(QtWidgets.QLayout.SetFixedSize)
0
Matteo Boscolo On

I solve the problem using the following code

def showMore(self):
    self._lineEditVisible = not self._lineEditVisible
    if self._lineEditVisible:
        self.lineEdit.setMinimumSize(600, 400)
        self.lineEdit.setMaximumHeight(12000)
        self.lineEdit.setMaximumWidth(12000)
        self.setMaximumSize(12000,12000)
    else:
        self.lineEdit.setMinimumSize(0, 0)
        self.setMaximumSize(1200, 100)
        self.lineEdit.setMaximumHeight(0)
        self.lineEdit.setMaximumWidth(0)