Detect resizing in Widget-window resized signal

后端 未结 1 1131
[愿得一人]
[愿得一人] 2020-12-06 19:03

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 :

相关标签:
1条回答
  • 2020-12-06 19:35

    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_())
    
    0 讨论(0)
提交回复
热议问题