Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyqt4 window resize event

I'm using python3 and pyqt4 and I want some code to run every time my QMainWindow is resized.I would like something like this

self.window.resized.connect(self.resize)

but resized is not a builtin function or method. Can anyone help.

like image 336
chicken-me Avatar asked Dec 11 '16 21:12

chicken-me


2 Answers

You must override the resizeEvent method.

from PyQt4 import QtCore, QtGui
import sys

class MainWindow(QtGui.QMainWindow):
    def resizeEvent(self, event):
        print("resize")
        QtGui.QMainWindow.resizeEvent(self, event)



app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
like image 64
eyllanesc Avatar answered Oct 10 '22 00:10

eyllanesc


For anyone who is looking for it, here is a PyQt5 solution. This is pretty much @eyllancesc code, so all thanks goes to them. There are only a few modifications needed for PyQt5

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class MainWindow(QtWidgets.QMainWindow):
    def resizeEvent(self, event):
        print("Window has been resized")
        QtWidgets.QMainWindow.resizeEvent(self, event)


app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
like image 40
Jacob Ward Avatar answered Oct 10 '22 01:10

Jacob Ward