使用Qt Designer为PySide设计用户界面

admin 2022年6月27日08:10:16评论97 views字数 3448阅读11分29秒阅读模式

Qt Designer中文名:Qt设计师,是一个可视化GUI设计工具。可以帮助我们加快编写Qt程序的速度。Qt Designer可以将设计好的用户界面保存为.ui文件,其实是XML格式的文本文件。

为了在PySide使用.ui文件,我们需要通过工具pyside-uic将.ui文件转换为.py文件,然后将转换后的.py文件引入我们自己的代码中。

下面是一个实际的例子,首先在Qt Designer中设计一个对话框,名为GoToCellDialog,并另存为 gotocelldialog.ui。

使用Qt Designer为PySide设计用户界面

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设计用户界面


原文始发于微信公众号(汇编语言):使用Qt Designer为PySide设计用户界面

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年6月27日08:10:16
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   使用Qt Designer为PySide设计用户界面http://cn-sec.com/archives/1145316.html

发表评论

匿名网友 填写信息