Qt Quick(QML)项目创建及运行

开发工具

建议使用VSCode,它有QML插件,IDEA的收费。

qrc文件的编译

https://www.psvmc.cn/article/2021-11-24-qt-quick-qrc.html

resources.qrc

1
2
3
4
5
6
7
8
<RCC>
<qresource prefix="/">
<file>imgs/app_min.png</file>
<file>js/common_methods.js</file>
<file>components/ZButton.qml</file>
<file>main.qml</file>
</qresource>
</RCC>

QML中使用

1
import "js/common_methods.js" as CommonMethods

图片

1
2
3
4
5
6
7
8
9
10
11
Image {
width: 100
height: 100
Layout.preferredWidth: 100
Layout.preferredHeight: 100
Layout.fillHeight: false
Layout.fillWidth: false
Layout.alignment: Qt.AlignHCenter
Layout.topMargin: 48
source: "qrc:/imgs/app_min.png"
}

编译

1
pyside2-rcc resources.qrc -o resources_rc.py

项目创建

项目建议使用pipenv管理依赖

Pipfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[[source]]
url = "https://mirrors.huaweicloud.com/repository/pypi/simple"
verify_ssl = false
name = "pip_conf_index_global"

[packages]
pyside2 = "==5.15.2.1"
pytwain = "==2.3.0"
opencv-python = "==4.5.4.60"
pyzbar = "==0.1.9"
tornado = "==6.4.2"
httpx = "==0.28.1"
pydantic = "==2.10.4"
numpy = "==1.24.4"
alibabacloud-ocr-api20210707 = "==3.1.2"

[dev-packages]

[requires]
python_version = "3.8.2"

打包

安装pyinstaller

1
pip install pyinstaller==6.11.1

一般打包

1
pyinstaller main.py -y --noconsole --name="xh-marking-client" --icon="logo.ico"

排除DLL

打包,同时会自动生成spec文件

1
pyinstaller main.py -y --noconsole --name="xh-marking-client" --icon="logo.ico"

修改spec文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- mode: python ; coding: utf-8 -*-

a = Analysis(
["main.py"],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
optimize=0,
)

exclude_dlls = ["Qt5WebEngineCore.dll"]
# 过滤掉要排除的DLL文件
filtered_binaries = []
for binary in a.binaries:
if not any(exclude_dll in binary[0] for exclude_dll in exclude_dlls):
filtered_binaries.append(binary)
a.binaries = filtered_binaries

pyz = PYZ(a.pure)

exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name="xhscanner-client",
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon=["logo.ico"],
)
coll = COLLECT(
exe,
a.binaries,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name="xh-marking-client",
)

使用修改后的spec文件打包

1
pyinstaller xh-marking-client.spec --clean -y

设置VSCode任务

项目根目录创建.vscode文件夹

添加文件tasks.json

先编译后执行脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"version": "2.0.0",
"tasks": [
{
"label": "编译",
"type": "shell",
"command": "pyside2-rcc resources.qrc -o resources_rc.py",
"problemMatcher": [],
},
{
"label": "主页面",
"type": "shell",
"command": "python main.py",
"dependsOn": [
"编译"
],
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

运行任务直接用快捷键Ctrl+Shift+B

运行时再点击快捷键Ctrl+Shift+B可以选择重启任务或者终止任务