2019-02-26 人工智能(编号01)刘老师博客

我创建一个简单的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__":
   importsys
   app =QtWidgets.QApplication(sys.argv)
   MainWindow = QtWidgets.QMainWindow()
   ui =Ui_MainWindow()
   ui.setupUi(MainWindow)
   MainWindow.show()
   sys.exit(app.exec_())

我发现了一个类似的问题QWidget调整大小信号?这个教程用来处理sizethat推荐的覆盖重新调整QMainWindow的事件方法。但他们中的任何一个都无法解决我的问题。是否存在检测窗口大小调整的功能:
回答1 默认情况下没有这样的信号,但你可以创建调整大小的信号,我们
在resizeEventfunction中发出它。

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_())
回答2:
You can override the resizeEvent by
def resizeEvent(self, newSize):    #do code here
回答3:
import sys
from PyQt5.QtWidgetsimport *
def onResize(event):
        print(event)
if __name__ =="__main__":
        app =  QApplication(sys.argv)
        widget =  QPushButton('Test')
        widget.resizeEvent = onResize
        widget.resize(640,480)
        widget.show()
        sys.exit(app.exec_())

你可能感兴趣的:(2019-02-26 人工智能(编号01)刘老师博客)