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
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
setSizeConstraintis 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 toSetNoConstraintwill not reset the above min/max sizes if those values have been explicitly set or implicitly set by a previoussetSizeConstraintcall.You also don't need to call
resizebased 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 byQtWidgets.QWIDGETSIZE_MAX(which is 16777215, 2^24).