【问题标题】:Detect resizing in Widget-window resized signal检测 Widget-window resized 信号中的调整大小
【发布时间】:2017-08-24 21:38:05
【问题描述】:

我使用 Qt Designer 创建了一个简单的 UI,并将其转换为 Python 代码。我搜索了任何方法来检测窗口大小的变化。

这是生成的代码:

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

我发现了一个类似的问题 QWidget resize signal?this tutorial to handle size 建议覆盖 QMainWindowresizeEvent 方法。

但其中任何一个都不能解决我的问题。是否有任何 resized 函数来检测窗口调整大小,如下所示:

MainWindow.resized.connect(self.someFunction)

【问题讨论】:

    标签: python pyqt pyqt5 qwidget


    【解决方案1】:

    默认没有这样的信号,但是你可以创建resized信号,我们在resizeEvent函数中发出它。

    例如:

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

    【讨论】:

    • 为什么是return super(...)resizeEvent 根据doc.qt.io/qt-5/qwidget.html#resizeEvent 不返回任何内容
    • @user136036 每个函数都会返回一些东西,即使那个东西是“无”的,例如你可以使用void foo(){ return; }或简单的void foo(){},使用或不使用return是风格和可选的什么都不返回的方法。
    • 这种没有return语句的函数称为void函数。无效函数是那些不返回任何内容的函数。 'None' 是 Python 中的一种特殊类型,在 void 函数结束后返回。所以从技术上讲,它总是会返回None,但实际上,它不会返回任何东西。
    猜你喜欢
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多