Qt开发
跨平台图形化框架
版本兼容
- Qt4.0和Qt5.0的API存在差异,此外python 平台下两个库的使用,导致 Qt 代码的兼容非常麻烦
- github上有开源脚本Qt,py脚本,使用后可兼容Qt4.0写法,以及PyQt和PySide不同平台的差异问题,但是会导致IDE的代码补全失效 )))
Qt实现hellow world界面窗口
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2.QtCore import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.setWindowTitle(u"欢迎使用 PySide2类 编写窗口")
self.button = QPushButton("hello,world")
self.button.clicked.connect(self.printHelloWorld)
self.layout = QHBoxLayout()
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.show()
def printHelloWorld(self):
print "hello,world"
a = Window()
- MEL或者maya cmds命令也可以实现同样效果,甚至更简单,Python Qt的框架优势在于:
1.QtDesigner组件拖拽式开发
2.组件封装扩展
3.多重事件响应触发
4.qss 自定义样式支持
5.自定义响应信号
ui文件
- 属性编辑器可以编辑UI的属性,但是通常是用代码来动态修改这些属性。代码调用会通过objectName的属性来调用。
- ctrl+s保存当前编辑的文件之后会输出一个后缀为ui的文件,直接打开会看到是一个XML文件(可以魔改出特殊的组件文件)
编译ui文件
安装相关的Python Qt库,在相关路径下找到编译程序
路径/user/AppData/Local/Programs/Python/Python38/Scripts/pyside2-uic.exe
命令行调用该程序,后面加-h可以显示使用方法
pyside2-uic /path/to/.ui -o /path/to/ui.py
基本写法:程序名 ui文件 -o 输出的python文件这样就编译好ui文件,需要注意PyQt的编译需要改为PySide的库进行导入,之后可以使用继承的方法来调用编译的ui文件:
#编译好的ui文件导入其中的类(类名和UI文件当中的最外层的组件的objectName一致)
from test_ui import Ui_Form
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2.QtCore import *
class window(Ui_Form,QWidget):
def __init__(self):
# 执行 QWidget 的 __init__
super(window,self).__init__()
# 从 Ui_Form 继承的方法,可以直接将ui生成出来
self.setupUi(self)
self.setWindowTitle(u"用 pyside2-uic 程序编译python 继承出来的窗口")
self.show()
win = window()作者:zhangjn 创建时间:2025-04-16 09:01
最后编辑:zhangjn 更新时间:2025-04-16 17:07
最后编辑:zhangjn 更新时间:2025-04-16 17:07