python中面向对象的ui_面向对象编程风格的Qt.ui文件的Python代码生成器?

我想知道如何从用QtDesigner创建的.ui文件生成OOP风格的Python代码。在

我想说的是,pyuic4生成的代码不是OOP风格的:小部件不会成为QtGui类的子类。代码很难阅读和扩展。在

我想要封装所有子部件、布局和函数的类。在

让我们看一个简单的例子。这个主窗口包含一个滑块和一个液晶显示器,它们以明显的方式连接在一起。在

首先,这是pyuic4生成的代码:from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):

def setupUi(self, MainWindow):

MainWindow.setObjectName("MainWindow")

MainWindow.resize(268, 186)

self.centralwidget = QtGui.QWidget(MainWindow)

self.centralwidget.setObjectName("centralwidget")

self.gridLayout = QtGui.QGridLayout(self.centralwidget)

self.gridLayout.setObjectName("gridLayout")

self.horizontalSlider = QtGui.QSlider(self.centralwidget)

self.horizontalSlider.setOrientation(QtCore.Qt.Vertical)

self.horizontalSlider.setObjectName("horizontalSlider")

self.gridLayout.addWidget(self.horizontalSlider, 0, 1, 1, 1)

self.lcdNumber = QtGui.QLCDNumber(self.centralwidget)

self.lcdNumber.setObjectName("lcdNumber")

self.gridLayout.addWidget(self.lcdNumber, 0, 2, 1, 1)

MainWindow.setCentralWidget(self.centralwidget)

self.menubar = QtGui.QMenuBar(MainWindow)

self.menubar.setGeometry(QtCore.QRect(0, 0, 268, 25))

self.menubar.setObjectName("menubar")

MainWindow.setMenuBar(self.menubar)

self.statusbar = QtGui.QStatusBar(MainWindow)

self.statusbar.setObjectName("statusbar")

MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)

QtCore.QObject.connect(self.horizontalSlider, QtCore.SIGNAL("valueChanged(int)"), self.lcdNumber.display)

QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):

MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))

if __name__ == "__main__":

import sys

app = QtGui.QApplication(sys.argv)

MainWindow = QtGui.QMainWindow()

ui = Ui_MainWindow()

ui.setupUi(MainWindow)

MainWindow.show()

sys.exit(app.exec_())

我手动将其编辑为我想要的样式:

^{pr2}$

这可以由代码生成器完成,而不需要枯燥的编辑。我的问题是:有没有这样的代码生成器可用?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当与Qt Designer一起使用时,您可以使用PyQt库创建一个类似于Windows资源管理器的程序。下面是一个示例代码,展示如何使用PythonQt Designer创建一个文件资源管理器: 首先,使用Qt Designer创建一个.ui文件,包含一个QTreeView和一个QListView。 然后,使用pyuic工具将.ui文件转换为Python代码。假设您的.ui文件名为file_explorer.ui,运行以下命令: ``` pyuic5 file_explorer.ui -o file_explorer_ui.py ``` 下面是一个示例代码,将UI文件Python代码结合起来: ```python import sys import os from PyQt5.QtWidgets import QApplication, QMainWindow, QFileSystemModel, QTreeView, QListView from file_explorer_ui import Ui_MainWindow class FileExplorer(QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) # 创建文件系统模型 self.file_model = QFileSystemModel() self.file_model.setRootPath("") # 设置模型到TreeView self.treeView.setModel(self.file_model) self.treeView.setRootIndex(self.file_model.index("")) # 设置模型到ListView self.listView.setModel(self.file_model) self.listView.setRootIndex(self.file_model.index("")) # TreeView和ListView的信号连接到槽函数 self.treeView.clicked.connect(self.on_treeView_clicked) self.listView.doubleClicked.connect(self.on_listView_doubleClicked) def on_treeView_clicked(self, index): # 获取所选项的路径 path = self.file_model.filePath(index) # 设置ListView的根目录 self.listView.setRootIndex(self.file_model.index(path)) def on_listView_doubleClicked(self, index): # 获取所选项的路径 path = self.file_model.filePath(index) # 如果是文件夹,则设置TreeView和ListView的根目录 if os.path.isdir(path): self.treeView.setRootIndex(index) self.listView.setRootIndex(index) if __name__ == "__main__": app = QApplication(sys.argv) window = FileExplorer() window.show() sys.exit(app.exec_()) ``` 确保将生成的file_explorer_ui.py文件与示例代码放在同一个目录下,并运行示例代码。 这样,您将会看到一个类似于Windows资源管理器的文件浏览器程序,在TreeView展示目录结构,在ListView展示所选目录的文件文件夹。您可以根据需要自定义UI和功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值