Qt开发-QT Widgets开发入门(开发工具、项目运行、事件绑定、打包)

前言

Qt QuickQt Widgets这两种技术,官方是强推Qt Quick的。

但是实际体验Qt Quick在桌面端的体验一般,并且国内大厂都还是用的Qt Widgets

https://download.qt.io/official_releases/QtForPython/

资源文件个人建议:

UI 文件单独放在ui文件夹中,使用VSCode任务自动生成Python文件。

qrc中只存放qss样式图片等资源文件。

操作qrc还是建议在Qt Creator中操作,比较方便。

开发工具

编辑UI文件使用 自带的 Qt Creator

项目使用VSCode,IDEA中的Qt插件是收费的。

Python格式化插件 安装Black Formatter

文件 => 首选项 => 设置

添加配置

1
2
3
4
5
6
7
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"editor.formatOnSave": true,
},

设置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可以选择重启任务或者终止任务

同时依赖UI和资源的编译

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
{
"version": "2.0.0",
"tasks": [
{
"label": "ui_form",
"type": "shell",
"command": "pyside2-uic ui/form.ui -o ui_form.py",
"problemMatcher": [],
},
{
"label": "编译",
"type": "shell",
"command": "pyside2-rcc resources.qrc -o resources_rc.py",
"problemMatcher": [],
},
{
"label": "主页面",
"type": "shell",
"command": "python main.py",
"dependsOn": [
"ui_form",
"编译"
],
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

加载UI

UI转Python(推荐)

转成代码后加载

UI文件转Python代码

1
pyside2-uic form.ui -o ui_form.py

显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import sys
from PySide2.QtWidgets import QApplication, QWidget
from ui_form import Ui_Widget


class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_Widget()
self.ui.setupUi(self)


if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

直接加载UI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import os
from pathlib import Path
import sys

from PySide2.QtWidgets import QApplication, QWidget,QVBoxLayout
from PySide2.QtCore import Signal,QObject
from PySide2.QtUiTools import QUiLoader


class MainWin(QObject):
def __init__(self):
self.load_ui()

def load_ui(self):
self.ui = QUiLoader().load("form.ui")


if __name__ == "__main__":
app = QApplication([])
mainWin = MainWin()
mainWin.ui.show();
sys.exit(app.exec_())

加载qrc中的UI(推荐)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# This Python file uses the following encoding: utf-8
import sys
from PySide2.QtWidgets import QApplication, QWidget
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtUiTools import QUiLoader
import resources_rc

class MainWin(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = QUiLoader().load(':/form.ui')
print(self.ui)

if __name__ == "__main__":
app = QApplication(sys.argv)
main_win = MainWin()
main_win.ui.show()
sys.exit(app.exec_())

设置样式

添加引用

1
2
from PySide2 import QtCore, QtGui, QtWidgets
import resources_rc

设置样式

1
2
3
4
5
6
7
8
9
# 读取资源文件中的 QSS 文件
qss_file = QtCore.QFile(":/qss/common.qss")
if qss_file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text):
qss_style = qss_file.readAll()
qss_style = str(qss_style, encoding='utf-8')
qss_file.close()

# 设置全局样式表
app.setStyleSheet(qss_style)

事件绑定

加载的代码

如果UI文件已经转为Python文件,那么组件都会添加到属性中,可以直接使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import sys
from PySide2.QtWidgets import QApplication, QWidget
from ui_form import Ui_Widget


class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_Widget()
self.ui.setupUi(self)
self.ui.loginBtn.clicked.connect(self.on_button_clicked)

def on_button_clicked(self):
print("登录!")

加载的UI文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# This Python file uses the following encoding: utf-8
import sys
from PySide2.QtWidgets import QApplication, QWidget
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtUiTools import QUiLoader
import resources_rc

class MainWin(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = QUiLoader().load(':/form.ui')

loginBtn = self.ui.findChild(QtWidgets.QPushButton, 'loginBtn')
loginBtn.clicked.connect(self.on_button_clicked)


def on_button_clicked(self):
print("登录!")

打包

Win上打包

1
pyinstaller main.py -y --windowed

设置ico

1
pyinstaller main.py -y --noconsole --name="xhschool" --icon="logo.ico"

单文件

1
pyinstaller main.py -y -F --noconsole --name="xhschool" --icon="logo.ico"

注意

在Win上--windowed--noconsole作用一样,但是--windowed只能在Win上用,--noconsole还可以在Linux上打包。