PySide: What is the best way to resize the main window if one widget is hidden?

What is the best way to automatically resize the main window if one widget is hidden? I also need to resize the window if I make the widget again. See images below ...

What settings should be set in Qt Designer for MainWindow (sizePolicy), centralwidget (sizePolicy, layoutSizeConstraint) and gridLayout (in particular, sizePolicy, layoutSizeConstraint)?

How to hide textEdit widget? self.textEditObject.setVisible(False) works: textEditObject is not displayed, but is this the right way to resize a window?

I think I don’t understand some basic things, because I can’t get the desired behavior.

enter image description here

+6
source share
3 answers

You can resize the window to minimumSizeHint() after hiding the widget:

 self.resize(minimumSizeHint()) 

This will reduce the window size to a minimum size.

If you only want to reduce the height, you can do something like:

 self.resize(width(), minimumSizeHint().height()) 

But you should keep in mind that the minimum size is not calculated until some events are processed in the event loop. So when you hide your widget, just handle the event loop for some iterations, and then resize to a minimum.

Like it:

 for i in range(0, 10): QApplication.processEvents() self.resize(width(), minimumSizeHint().height()) 

Another option is a single QTimer snapshot, which triggers a slot in which you resize the window to a minimum. Thus, when the window is resized, the minimum size hint is calculated correctly.

+3
source

Even easier than manual size, QWidget.adjustSize () .

Here is an example:

 from PySide import QtGui def hide_show(): x.setVisible(not x.isVisible()) # toggles visibility of the label w.adjustSize() # adjusts size of widget app = QtGui.QApplication([]) w = QtGui.QWidget() l = QtGui.QVBoxLayout(w) b = QtGui.QPushButton('Click me') l.addWidget(b) x = QtGui.QLabel('Some Text') l.addWidget(x) b.clicked.connect(hide_show) w.show() app.exec_() 

If you have a QMainWidget , I really managed to compress it a bit, but not completely. Perhaps better than other solutions.

 from PySide import QtGui def hide_show(): x.setVisible(not x.isVisible()) # toggles visibility of the label w2.adjustSize() # adjusts size of widget w.adjustSize() # adjusts size of main window app = QtGui.QApplication([]) w = QtGui.QMainWindow() w2 = QtGui.QWidget() l = QtGui.QVBoxLayout(w2) b = QtGui.QPushButton('Click me') l.addWidget(b) x = QtGui.QTextEdit('Some Text') l.addWidget(x) b.clicked.connect(hide_show) w.setCentralWidget(w2) w.show() app.exec_() 
+1
source

OK Below is the code for testing the QTimer approach proposed by Nejat, and based on the Trilarions example. Although I don't know if this is what Nejat means it works best for me.

 from PySide import QtCore, QtGui class TestWin(QtGui.QMainWindow): def __init__(self): super(TestWin, self).__init__() self.w2 = QtGui.QWidget() self.l = QtGui.QVBoxLayout(self.w2) self.h = QtGui.QHBoxLayout(self.w2) self.b = QtGui.QPushButton('hierarchic adjustSize()') self.b2 = QtGui.QPushButton('deferred resize()') self.h.addWidget(self.b) self.h.addWidget(self.b2) self.l.addLayout(self.h) self.x = QtGui.QTextEdit('Some Text') self.l.addWidget(self.x) self.b.clicked.connect(self.adjustSize_hide_show) self.b2.clicked.connect(self.qtimer_hide_show) self.setCentralWidget(self.w2) def toggleVis(self): self.x.setVisible(not self.x.isVisible()) def adjustSize_hide_show(self): self.toggleVis() self.w2.adjustSize() # adjusts size of widget self.adjustSize() # adjusts size of main window def qtimer_hide_show(self): self.toggleVis() _timer = QtCore.QTimer() _timer.singleShot(30, self._resizeHeight) def _resizeHeight(self): self.resize(self.width(), self.minimumSizeHint().height()) if __name__ == '__main__': app = QtGui.QApplication([]) win = TestWin() win.show() app.exec_() 

Thus, you do not need acces for the application, and you can actually resize in a certain direction.

It’s not very convenient for me to rely on the delay here. But it still works like 90% of the time with a 1 ms delay! While I tried 2 ms, it was a 100% success. Thus, 20 ms will undoubtedly cause very little flicker in almost all cases (possibly depending on your layout too)

0
source

Source: https://habr.com/ru/post/981686/


All Articles