Qt Designer中文名:Qt设计师,是一个可视化GUI设计工具。可以帮助我们加快编写Qt程序的速度。Qt Designer可以将设计好的用户界面保存为.ui文件,其实是XML格式的文本文件。
为了在PySide使用.ui文件,我们需要通过工具pyside-uic将.ui文件转换为.py文件,然后将转换后的.py文件引入我们自己的代码中。
下面是一个实际的例子,首先在Qt Designer中设计一个对话框,名为GoToCellDialog,并另存为 gotocelldialog.ui。
gotocelldialog.ui文件内容:
<ui version="4.0">
<class>GoToCellDialogclass>
<widget class="QDialog" name="GoToCellDialog">
<property name="geometry">
<rect>
<x>0x>
<y>0y>
<width>226width>
<height>70height>
rect>
property>
<property name="windowTitle">
<string>Go to Cellstring>
property>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>98x>
<y>8y>
<width>119width>
<height>20height>
rect>
property>
widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>8x>
<y>8y>
<width>84width>
<height>20height>
rect>
property>
<property name="text">
<string>&Cell Location:string>
property>
<property name="buddy">
<cstring>lineEditcstring>
property>
widget>
<widget class="QPushButton" name="okButton">
<property name="geometry">
<rect>
<x>58x>
<y>38y>
<width>77width>
<height>25height>
rect>
property>
<property name="text">
<string>Okstring>
property>
widget>
<widget class="QPushButton" name="cancelButton">
<property name="geometry">
<rect>
<x>141x>
<y>38y>
<width>77width>
<height>25height>
rect>
property>
<property name="text">
<string>Cancelstring>
property>
widget>
widget>
<resources/>
<connections/>
接下来使用pyside-uic转换.ui文件:
pyside-uic gotocelldialog.ui -o gotocelldialog_ui.py
gotocelldialog_ui.py文件内容:
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'gotocelldialog.ui'
#
# Created: Wed Oct 27 17:13:19 2010
# by: PySide uic UI code generator
#
# WARNING! All changes made in this file will be lost!from PySide import QtCore, QtGui
class Ui_GoToCellDialog(object):
def setupUi(self, GoToCellDialog):
GoToCellDialog.setObjectName("GoToCellDialog")
GoToCellDialog.resize(226, 70)
self.lineEdit = QtGui.QLineEdit(GoToCellDialog)
self.lineEdit.setGeometry(QtCore.QRect(98, 8, 119, 20))
self.lineEdit.setObjectName("lineEdit")
self.label = QtGui.QLabel(GoToCellDialog)
self.label.setGeometry(QtCore.QRect(8, 8, 84, 20))
self.label.setObjectName("label")
self.okButton = QtGui.QPushButton(GoToCellDialog)
self.okButton.setGeometry(QtCore.QRect(58, 38, 77, 25))
self.okButton.setObjectName("okButton")
self.cancelButton = QtGui.QPushButton(GoToCellDialog)
self.cancelButton.setGeometry(QtCore.QRect(141, 38, 77, 25))
self.cancelButton.setObjectName("cancelButton")
self.label.setBuddy(self.lineEdit)
self.retranslateUi(GoToCellDialog)
QtCore.QMetaObject.connectSlotsByName(GoToCellDialog)
def retranslateUi(self, GoToCellDialog):
GoToCellDialog.setWindowTitle(QtGui.QApplication.translate("GoToCellDialog", "Go to Cell", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("GoToCellDialog", "&Cell Location:", None, QtGui.QApplication.UnicodeUTF8))
self.okButton.setText(QtGui.QApplication.translate("GoToCellDialog", "Ok", None, QtGui.QApplication.UnicodeUTF8))
self.cancelButton.setText(QtGui.QApplication.translate("GoToCellDialog", "Cancel", None, QtGui.QApplication.UnicodeUTF8))
然后编写代码,引用gotocelldialog_ui.py,并显示GoToCellDialog。代码如下:
1 #!/usr/bin/env python 2 3 import sys
4 5 from PySide.QtCore import* 6 from PySide.QtGui import* 7 8 from gotocelldialog_ui import Ui_GoToCellDialog
9 10 class GoToCellDialog(QDialog):
11 def__init__(self, parent=None):
12 QDialog.__init__(self, parent)
13 14 self.ui= Ui_GoToCellDialog()
15 self.ui.setupUi(self)
16 17 def main():
18 app = QApplication(sys.argv)
19 d = GoToCellDialog()
20 d.show()
21 sys.exit(app.exec_())
22 23 if__name__== '__main__':
24 main()
最终的效果图:
原文始发于微信公众号(汇编语言):使用Qt Designer为PySide设计用户界面
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论