pyqt5迁移pysd6

本文详细记录了从PyQt5迁移到Pyside6时遇到的问题,包括代码调整、信号处理、UI工具替换等内容,提供了一个实用的迁移教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考文章:

pyqt5迁移pysd6问题记录_pyqt5的代码转为pyside6-CSDN博客

Python 图形界面框架 PySide6 使用及避坑指南-CSDN博客

  1. 安装pyside6
  2. 把PyQt5全部替换为PySide6
  3. pyqtSignal()全部替换为Signal()
  4. 把ui2py的 pyuic5.exe 改为 pyside6-uic.exe
  5. 把 QRegExpValidator、QRegExp,全部替换为 QRegularExpressionValidator、QRegularExpression

自适应pyqt5和pyside6的方式

(这里假定之前大面积使用pyqt5,现需要切换pyside6)

file: pyside_check.py

HAVE_PYSIDE6 = False
try:
    from PySide6 import *
    HAVE_PYSIDE6 = True
except ImportError:
    HAVE_PYSIDE6 = False

要修改的文件:

import pyside_check

if pyside_check.HAVE_PYSIDE6:
    from PySide6.QtCore import Signal as pyqtSignal
    from PySide6.QtCore import QRegularExpression as QRegExp
    from PySide6.QtGui import QRegularExpressionValidator as QRegExpValidator
    from PySide6.QtWidgets import QApplication

    from ui_main_window_6 import Ui_MainWindow
else:
    from PyQt5.QtCore import QRegExp, pyqtSignal
    from PyQt5.QtGui import QRegExpValidator
    from PyQt5.QtWidgets import QApplication

    from ui_main_window import Ui_MainWindow

ui2py.py:

import os
import subprocess

# pyuic5 的路径
PYUIC5_PATH = r'D:\Python39\Scripts\pyuic5.exe'
PySide6_PATH = r'D:\Python39\Scripts\pyside6-uic.exe'

# 遍历的根目录路径
ROOT_DIR = 'xxxxxxxx'


def convert_ui_to_py(ui_file_path, py_file_path):
    try:
        # 使用 subprocess 调用 pyuic5
        subprocess.run([PYUIC5_PATH, ui_file_path, "-o", f'{py_file_path}.py'], check = True)
        subprocess.run([PySide6_PATH, ui_file_path, "-o", f'{py_file_path}_6.py'], check = True)
        print(f"【成功转换】 {ui_file_path}")
    except subprocess.CalledProcessError as e:
        print(f"【转换 {ui_file_path} 失败: {e}】")


def traverse_dir(root_dir):
    """遍历目录及其子目录,转换 .ui 文件为 .py 文件"""
    for dirpath, dirnames, filenames in os.walk(root_dir):
        for filename in filenames:
            if filename.endswith('.ui'):
                ui_file_path = os.path.join(dirpath, filename)
                py_file_path = os.path.join(dirpath, filename[:-3])  # 移除 .ui 后缀并添加 .py
                convert_ui_to_py(ui_file_path, py_file_path)

def runMain():
    print("开始转换...")
    traverse_dir(ROOT_DIR)
    print("转换完成!")

其他不兼容的地方:

# pyside的字体不支持setWeight
font.setWeight(65)
    if pyside_check.HAVE_PYSIDE6:
        sys.exit(app.exec())
    else:
        sys.exit(app.exec_())
# pyside没有desktop()
screen = QApplication.desktop().screenGeometry()

#改为
        if pyside_check.HAVE_PYSIDE6:
            app = QApplication.instance()
            screen = app.primaryScreen()
            screen_geometry = screen.geometry()
            self.w = screen_geometry.width()
            self.h = screen_geometry.height()
        else:
            screen = QApplication.desktop().screenGeometry()
            self.w = screen.width()
            self.h = screen.height()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值