我想知道如何使用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_())
我手动将其编辑为我想要的样式:
from PyQt4 import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setObjectName("MainWindow")
self.resize(268, 186)
self.centralwidget = QtGui.QWidget(self)
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)
self.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(self)
self.menubar.setGeometry(QtCore.QRect(0, 0, 268, 25))
self.menubar.setObjectName("menubar")
self.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(self)
self.statusbar.setObjectName("statusbar")
self.setStatusBar(self.statusbar)
self.retranslateUi()
QtCore.QObject.connect(self.horizontalSlider, QtCore.SIGNAL("valueChanged(int)"), self.lcdNumber.display)
QtCore.QMetaObject.connectSlotsByName(self)
def retranslateUi(self):
self.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
这个可以由代码生成器完成而无需枯燥的编辑。我的问题是:是否有一个这样的代码生成器?
答案 0 :(得分:2)
您可以使用多重继承(请参阅Using Qt Designer in PyQt4了解可能的方法):
from PyQt4.QtGui import QDialog
from ui_imagedialog import Ui_ImageDialog
class ImageDialog(QDialog, Ui_ImageDialog):
def __init__(self):
QDialog.__init__(self)
# Set up the user interface from Designer.
self.setupUi(self)
# Make some local modifications.
self.colorDepthCombo.addItem("2 colors (1 bit per pixel)")
# Connect up the buttons.
self.okButton.clicked.connect(self.accept)
self.cancelButton.clicked.connect(self.reject)
尽管如此,这两种方法都是OOP风格。他们每个人都有利弊。
不要读取或编辑自动生成的文件。
答案 1 :(得分:1)
您甚至不必从ui文件生成python文件:使用uic
模块
import os
from PyQt4 import QtGui, QtCore, uic
class MyMainWindow(QtGui.QMainWindow):
def __init__(self, parent):
QtGui.QMainWindow.__init__(self, parent)
# We want to inheritate from MyMainWindow.ui
uic.loadUi(os.path.join(os.path.dirname(os.path.abspath(__file__)),"MyMainWindow.ui"), self)
# All widgets are available as classic attributes:
self.okButton.clicked.connect(self.accept)