chicken-me
chicken-me

Reputation: 141

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.

Upvotes: 10

Views: 20862

Answers (2)

Jacob Ward
Jacob Ward

Reputation: 219

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_())

Upvotes: 7

eyllanesc
eyllanesc

Reputation: 243887

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_())

Upvotes: 20

Related Questions