11

I create a simple UI with Qt Designer and convert it to Python codes. I searched for any method to detect changing window size.

This is the generated code :

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def onResize(event):
        print(event)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowTitle("MainWindow")
        MainWindow.resize(200, 200)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)

        MainWindow.resized.connect(self.someFunction)

        QtCore.QMetaObject.connectSlotsByName(MainWindow)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

I found a similar question QWidget resize signal? and this tutorial to handle size that recommended overriding resizeEvent method of QMainWindow.

But any of them doesn't solve my problem. Is there any resized function to detect window resizing like below:

MainWindow.resized.connect(self.someFunction)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Mahmood Kohansal
  • 1,021
  • 2
  • 16
  • 42

1 Answers1

24

There is no such signal by default, but you can create the resized signal, we emit it in the resizeEvent function.

For Example:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowTitle("MainWindow")
        MainWindow.resize(200, 200)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


class Window(QtWidgets.QMainWindow):
    resized = QtCore.pyqtSignal()
    def  __init__(self, parent=None):
        super(Window, self).__init__(parent=parent)
        ui = Ui_MainWindow()
        ui.setupUi(self)
        self.resized.connect(self.someFunction)

    def resizeEvent(self, event):
        self.resized.emit()
        return super(Window, self).resizeEvent(event)

    def someFunction(self):
        print("someFunction")


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Why the `return super(...)`? `resizeEvent` does not return anything according to https://doc.qt.io/qt-5/qwidget.html#resizeEvent – user136036 Jan 14 '20 at 17:53
  • @user136036 Every function returns something even if that something is "nothing", for example you can use `void foo(){ return; }` or simply `void foo(){}`, the use or not use of return is stylistic and optional in a method that returns nothing. – eyllanesc Jan 14 '20 at 17:56
  • Such functions which do not have a return statement in them are called void functions. Void functions are those that do not return anything. 'None' is a special type in Python which is returned after a void function ends. So technically, it would always return `None`, but really, it doesn't return anything. – QuirkyBit Mar 16 '20 at 09:24