为了账号安全,请及时绑定邮箱和手机立即绑定

PySide2 (Qt for Python) 如何在 QMainWindow 子类中加载 .ui

PySide2 (Qt for Python) 如何在 QMainWindow 子类中加载 .ui

元芳怎么了 2023-07-11 16:44:27
我正在尝试使用 Qt Creator 和 Qt for Python 为新应用程序创建快速原型。但是,向导生成的代码对我来说没有多大意义。这是main.pyQt Creator 生成的文件:# This Python file uses the following encoding: utf-8import sysimport osfrom PySide2.QtWidgets import QApplication, QMainWindowfrom PySide2.QtCore import QFilefrom PySide2.QtUiTools import QUiLoaderclass MyMainWindow(QMainWindow):    def __init__(self):        super(MyMainWindow, self).__init__()        self.load_ui()    def load_ui(self):        loader = QUiLoader()        path = os.path.join(os.path.dirname(__file__), "form.ui")        ui_file = QFile(path)        ui_file.open(QFile.ReadOnly)        loader.load(ui_file, self)        ui_file.close()if __name__ == "__main__":    app = QApplication([])    widget = MyMainWindow()    widget.show()    sys.exit(app.exec_())这是一个简单的form.ui,只有一个按钮:<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>MyMainWindow</class> <widget class="QMainWindow" name="MyMainWindow">  <property name="geometry">   <rect>    <x>0</x>    <y>0</y>    <width>800</width>    <height>600</height>   </rect>  </property>  <property name="windowTitle">   <string>MyMainWindow</string>  </property>  <widget class="QWidget" name="centralwidget">   <layout class="QVBoxLayout" name="verticalLayout">    <item>     <widget class="QPushButton" name="pushButton">      <property name="text">       <string>PushButton</string>      </property>     </widget>    </item>   </layout>  </widget>  <widget class="QMenuBar" name="menubar">   <property name="geometry">    <rect>     <x>0</x>     <y>0</y>     <width>800</width>     <height>22</height>    </rect>   </property>  </widget>  <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/></ui>我只是不明白类load_ui内函数的用途MyMainWindow。我知道 PyQt5 可以.ui在使用该模块的小部件初始化期间加载文件uic。据我所知,这对于 Python 的 Qt 来说是不可能的。我错过了什么吗?我正在使用 Qt Creator 4.13。
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 129 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信