PyQt qrc2py 使用PowerShell将qrc文件转为py文件并且将导入模块PyQt或PySide转换为qtpy模块开箱即用

前言

由于需要使用不同的qt环境(PySide,PyQt)所以写了这个脚本,使用找到的随便一个rcc命令去转换qrc文件,然后将导入模块换成qtpy这个通用库(支持pyside2-6,pyqt5-6),老版本的是Qt.py(支持pyside0-2~pyqt4-5)你可以自己改。

使用

上一篇文章写了如何将ui转为py 链接 这一篇换个写法

因为qrc文件一般只有一个,不像ui文件有很多个,所以在这里直接指定文件,不去查询所有的qrc文件

powershell 复制代码
# .LINK https://blog.csdn.net/weixin_42579717/article/details/137281606
# 检查rcc命令是否存在
$_commands = @("pyrcc5", "pyside2-rcc", "pyrcc6", "pyside6-rcc", "pyside-rcc", "pyrcc")
$command = $null
foreach ($cmd in $_commands)
{
    $command = Get-Command $cmd -ErrorAction SilentlyContinue
    if ($command)
    {
        break
    }
}
if (-not$command)
{
    Write-Host "Command not found."
    exit 1
}
$command = $command.Name
Write-Output "Use command: $command"

# 获取当前脚本所在的文件夹
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition

$qrcFile = Join-Path $scriptDirectory "resource.qrc"
$pyFile = Join-Path $scriptDirectory "resource_rc.py"

Write-Host $qrcFile
Write-Host $pyFile
& $command -o $pyFile $qrcFile
# 检查文件转换是否成功
if (Test-Path $pyFile)
{
    # 更换导入模块
    (Get-Content $pyFile -Encoding UTF8) -replace "from (PyQt[456]?|PySide[26]?)", "from qtpy" | Set-Content $pyFile -Encoding UTF8
    Write-Host "Files have been converted and modified successfully: $pyFile"
}
else
{
    Write-Host "convert failed: $qrcFile"
}

如果你的项目有结构的话比如下图

可以将以下代码替换

powershell 复制代码
# 获取当前脚本所在的文件夹
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition
$parentDirectory = Split-Path -Parent $scriptDirectory

$qrcFile = Join-Path $scriptDirectory "resource.qrc"
$pyFile = Join-Path $parentDirectory "ui/resource_rc.py"

PS

补充说明,PowerShell 不能用中文文档注释但是可以单行注释

相关推荐
云空7 小时前
《PyQt6-3D应用开发技术文档》
3d·pyqt
sword devil90010 小时前
PYQT实战:无刷电机模拟(只是模拟,没有写接口接收外部数据)
pyqt
sword devil9004 天前
PYQT实战:智能家居中控
python·智能家居·pyqt
OICQQ676580088 天前
创建一个基于YOLOv8+PyQt界面的驾驶员疲劳驾驶检测系统 实现对驾驶员疲劳状态的打哈欠检测,头部下垂 疲劳眼睛检测识别
yolo·pyqt·疲劳驾驶·检测识别·驾驶员检测·打哈欠检测·眼睛疲劳
mahuifa17 天前
PySide环境配置及工具使用
python·qt·环境配置·开发经验·pyside
小灰灰搞电子17 天前
Qt PyQt与PySide技术-C++库的Python绑定
c++·qt·pyqt
越甲八千18 天前
pyqt 简单条码系统
数据库·microsoft·pyqt
重生之我在厦门做测试20 天前
基于pyqt开发串口和对应映射值(path)的显示工具
pyqt
hvinsion23 天前
【开源解析】基于Python+Qt打造智能应用时长统计工具 - 你的数字生活分析师
python·pyqt·开源软件·应用时长统计
毕设做完了吗?25 天前
基于YOLOv8+Deepface的人脸检测与识别系统
python·yolo·毕业设计·pyqt