sa_penguin
sa_penguin

Reputation: 457

PyQt5 MainWindow resize() call not working

I have a PyQt5 GUI application mainwindow that sets geometry based on the screen size. When I call the toogleLogWindow() function, the visibility property of hLayoutWidget_error changes, but window resize does not happen. When I restore the mainwindow manually by clicking the restore button on the right top corner, the resize function works. Can anyone help me understand this behavior? actionToggleLogWindow status is not checked by default.

import sys, os

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUI()

    def setupUI(self):
               
        # Set screen size parameters
        for i in range(QApplication.desktop().screenCount()):
            self.window_size = QApplication.desktop().availableGeometry(i).size()
        self.resize(self.window_size)
        self.move(QPoint(0, 0))
        

        self._button = QtWidgets.QPushButton(self)
        self._button.setText('Test Me')
        
        self._editText = QtWidgets.QComboBox(self)
        self._editText.setEditable(True)
        self._editText.addItem("")
        self._editText.setGeometry(QtCore.QRect(240, 40, 113, 21))

        # Connect signal to slot
        self._button.clicked.connect(self.toogleLogWindow)
        
        
    def toogleLogWindow(self):
    
        if self._editText.currentText() == "0":
            h = self.window_size.height()
            w = int(self.window_size.width()/2)
            self.resize(w,h)
        elif self._editText.currentText() == "1":
            h = self.window_size.height()
            w = int(self.window_size.width())
            self.resize(w,h)
        else:
            pass
        
            
def get_main_app(argv=[]):
    app = QApplication(argv)
    win = MainWindow()
    win.show()
    return app, win

def main():
    app, _win = get_main_app(sys.argv)
    return app.exec_()

if __name__ == '__main__':
    sys.exit(main())

Upvotes: 0

Views: 1011

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

It should be noted that:

  • It seems that if setting the maximum size of a window before being shown and then displaying it is equivalent to maximizing the window.

  • When a window is maximized you cannot change its size unless you return it to the previous state, for example if you change the size of the window manually until it is in the normal state then you can just change the size.

So there are several alternatives for this case:

  • Do not set the full size of the screen:

    self.window_size = QApplication.desktop().availableGeometry(i).size() - QSize(10, 10)
    
  • Set the size after displaying:

    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUI()
    
        def setupUI(self):
            # Set screen size parameters
            for i in range(QApplication.desktop().screenCount()):
                self.window_size = QApplication.desktop().availableGeometry(i).size()
    
            self._button = QPushButton(self)
            self._button.setText("Test Me")
    
            self._editText = QComboBox(self)
            self._editText.setEditable(True)
            self._editText.addItem("")
            self._editText.setGeometry(QRect(240, 40, 113, 21))
    
            # Connect signal to slot
            self._button.clicked.connect(self.toogleLogWindow)
    
        def init_geometry(self):
            self.resize(self.window_size)
            self.move(QPoint(0, 0))
    
        def toogleLogWindow(self):
            if self._editText.currentText() == "0":
                h = self.window_size.height()
                w = int(self.window_size.width() / 2)
                self.resize(w, h)
            elif self._editText.currentText() == "1":
                h = self.window_size.height()
                w = int(self.window_size.width())
                self.resize(w, h)
            else:
                pass
    
    
    def get_main_app(argv=[]):
        app = QApplication(argv)
        win = MainWindow()
        win.show()
        win.init_geometry()
        return app, win
    

Upvotes: 2

Related Questions