大家好,这里是程序员晚枫,正在all in AI编程实战。
上一篇文章,给大家分析了PyQt5和PySide2之间的主要区别:https://www.python4office.cn/python-office/ref/250708-qt-pyside/
我也打算,接下来和团队一起,把现有的gui都换成PySide2。
今天给大家分享一下我改动第一套代码的过程,改完的代码在:发票识别的源码
将代码从 PyQt5 转换为 PySide2,主要涉及以下步骤:
1. 导入模块的修改
- PyQt5 使用
from PyQt5.QtWidgets import ... 等导入方式。 - PySide2 使用
from PySide2.QtWidgets import ... 等导入方式。
示例:
1 2 3 4 5 6 7
| from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton from PyQt5.QtCore import Qt
from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton from PySide2.QtCore import Qt
|
2. 信号和槽的连接方式
- PyQt5 使用
signal.connect(slot)。 - PySide2 使用
signal.connect(slot),但需要注意信号和槽的定义方式。
示例:
1 2 3 4 5
| button.clicked.connect(self.on_button_clicked)
button.clicked.connect(self.on_button_clicked)
|
如果使用了 pyqtSignal,需要替换为 Signal:
1 2 3 4 5 6 7 8 9 10 11
| from PyQt5.QtCore import pyqtSignal
class MyClass(QObject): my_signal = pyqtSignal()
from PySide2.QtCore import Signal
class MyClass(QObject): my_signal = Signal()
|
3. 事件处理
- PyQt5 和 PySide2 的事件处理方式基本一致,但需要注意事件过滤器的使用。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13
| def eventFilter(self, obj, event): if obj == self.widget and event.type() == QEvent.MouseMove: print("Mouse moved") return True return super().eventFilter(obj, event)
def eventFilter(self, obj, event): if obj == self.widget and event.type() == QEvent.MouseMove: print("Mouse moved") return True return super().eventFilter(obj, event)
|
4. 资源文件的加载
- PyQt5 使用
QResource 或 PyQt5.uic.loadUi。 - PySide2 使用
QResource 或 PySide2.QtUiTools.QUiLoader。
示例:
1 2 3 4 5 6 7 8
| from PyQt5 import uic self.ui = uic.loadUi("my_ui_file.ui", self)
from PySide2.QtUiTools import QUiLoader loader = QUiLoader() self.ui = loader.load("my_ui_file.ui", self)
|
5. 样式表的设置
- PyQt5 和 PySide2 的样式表设置方式一致。
示例:
1 2 3 4 5
| self.setStyleSheet("QPushButton { background-color: red; }")
self.setStyleSheet("QPushButton { background-color: red; }")
|
6. 其他注意事项
- 版本兼容性:确保 PySide2 的版本与 Qt 的版本兼容。
- 依赖库:PySide2 的依赖库可能与 PyQt5 不同,需要安装对应的依赖。
- 文档和社区支持:PySide2 的社区支持可能不如 PyQt5 丰富,需要多查阅官方文档。
示例:完整代码转换
以下是一个简单的 PyQt5 示例代码及其转换为 PySide2 的版本。
PyQt5 示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton from PyQt5.QtCore import Qt
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("PyQt5 Example") self.setGeometry(100, 100, 300, 200) self.button = QPushButton("Click Me", self) self.button.clicked.connect(self.on_button_clicked)
def on_button_clicked(self): print("Button clicked!")
if __name__ == "__main__": app = QApplication([]) window = MainWindow() window.show() app.exec_()
|
转换为 PySide2 的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton from PySide2.QtCore import Qt
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("PySide2 Example") self.setGeometry(100, 100, 300, 200) self.button = QPushButton("Click Me", self) self.button.clicked.connect(self.on_button_clicked)
def on_button_clicked(self): print("Button clicked!")
if __name__ == "__main__": app = QApplication([]) window = MainWindow() window.show() app.exec_()
|
总结
- 导入模块:将
PyQt5 替换为 PySide2。 - 信号和槽:确保信号和槽的定义和连接方式正确。
- 事件处理和资源加载:根据需要调整事件处理和资源加载方式。
- 其他细节:注意版本兼容性和依赖库的安装。
如果代码中使用了 PyQt5 的特定功能(如 pyqtProperty 等),需要查找 PySide2 的对应实现或替代方案。
大家在学习课程中有任何问题,欢迎+微信和我交流~我的联系方式:微信、读者群、1对1、福利



相关阅读
程序员晚枫专注AI编程培训,小白看完他和图灵社区合作的教程《30讲 · AI编程训练营》就能上手做AI项目。
🎓 AI 编程实战课程
想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!