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

使用Qt Designer为PySide设计用户界面 - Sean Lv - 博客园

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

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

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

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

gotocelldialog.ui文件内容:

代码
 
         
<? xml version="1.0" encoding="UTF-8" ?>
< ui version ="4.0" >
< class > GoToCellDialog </ class >
< widget class ="QDialog" name ="GoToCellDialog" >
< property name ="geometry" >
< rect >
< x > 0 </ x >
< y > 0 </ y >
< width > 226 </ width >
< height > 70 </ height >
</ rect >
</ property >
< property name ="windowTitle" >
< string > Go to Cell </ string >
</ property >
< widget class ="QLineEdit" name ="lineEdit" >
< property name ="geometry" >
< rect >
< x > 98 </ x >
< y > 8 </ y >
< width > 119 </ width >
< height > 20 </ height >
</ rect >
</ property >
</ widget >
< widget class ="QLabel" name ="label" >
< property name ="geometry" >
< rect >
< x > 8 </ x >
< y > 8 </ y >
< width > 84 </ width >
< height > 20 </ height >
</ rect >
</ property >
< property name ="text" >
< string > &amp; Cell Location: </ string >
</ property >
< property name ="buddy" >
< cstring > lineEdit </ cstring >
</ property >
</ widget >
< widget class ="QPushButton" name ="okButton" >
< property name ="geometry" >
< rect >
< x > 58 </ x >
< y > 38 </ y >
< width > 77 </ width >
< height > 25 </ height >
</ rect >
</ property >
< property name ="text" >
< string > Ok </ string >
</ property >
</ widget >
< widget class ="QPushButton" name ="cancelButton" >
< property name ="geometry" >
< rect >
< x > 141 </ x >
< y > 38 </ y >
< width > 77 </ width >
< height > 25 </ height >
</ rect >
</ property >
< property name ="text" >
< string > Cancel </ string >
</ property >
</ widget >
</ widget >
< resources />
< connections />
</ ui >

 

接下来使用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()
复制代码

 

最终的效果图:

posted on 2012-03-11 23:48  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/03/11/2391133.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt Designer设置界面为文的步骤如下: 1. 首先,获取Qt Designer的简体文语言包。将语言包放在Qt Designer目录下的translations文件夹。如果没有该文件夹,可以自己创建一个。 2. 然后,重新打开Qt Designer工具,界面就会变成文了。 3. 另外,如果想要设置字体,可以根据个人喜好进行设置。 需要注意的是,以上步骤的文件路径可能需要根据实际情况进行修改。具体路径可以参考引用\[1\]和引用\[2\]提供的示例路径。 #### 引用[.reference_title] - *1* [PyQt5 图形界面 - Qt Designer设置简体文方法演示,Qt Designer字体设置,Qt Designer工具单独安装包获取...](https://blog.csdn.net/qq_38161040/article/details/119065057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [如何将PyQt(pyqt-tools)Qt Designer改为文界面(汉化)](https://blog.csdn.net/NoamaNelson/article/details/103055449)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值