1 Your First QtWidgets Application
https://doc.qt.io/qtforpython-6/tutorials/basictutorial/widgets.html
we call app.exec() to enter the Qt main loop and start to execute the Qt code. app.exec()进入到QT的主循环内,并且执行了QT的code。
2 Using a Simple Button
https://doc.qt.io/qtforpython-6/tutorials/basictutorial/clickablebutton.html
Signals and slots is a Qt feature that lets your graphical widgets communicate with other graphical widgets or your python code. Our application creates a button that logs the Button clicked, Hello! message to the python console each time you click it.
signals和slots是QT的特点,作用在widgets上在图形widgets之间沟通
还是必须使用QApplication来运行QT code
The QPushButton has a predefined signal called clicked, which is triggered every time the button is clicked. We’ll connect this signal to the say_hello() function
已经定义好了信号signal叫做clicked,按钮被点击会被触发,connect到被slot装饰的函数say_hello
import sys
from PySide6.QtWidgets import QApplication, QPushButton
from PySide6.QtCore import Slot
@Slot() #装饰器,用来将下方的函数传递到另一个函数(类)Slot内,
def say_hello():
print("Button clicked, Hello!")
# Create the Qt Application
app = QApplication(sys.argv)
# Create a button, connect it and show it
button = QPushButton("Click me") # 可以点击的按钮
button.clicked.connect(say_hello) # 已经定义好了信号signal叫做clicked,按钮被点击会被触发,connect到被slot装饰的函数say_hello
button.show()
# Run the main Qt loop
app.exec()
3 Signals and Slots
Signals更像是开关,slots更像是开关变换的结果
While developing interfaces, you can get a real example by the effect of clicking a button: the ‘click’ will be the signal, and the slot will be what happens when that button is clicked, like closing a window, saving a document, etc.
点击是信号signals,点击以后发生的事件结果就是 slot
和回调函数类似,但是不相同的,所有继承自QObject类的子类,都可以包含Signals and Slots。当状态改变时就可以提交Signals,所有对象使用Signals来沟通,并不需要知道也不关心是否有其他对象接收这个signals,只需要发送signals就行
Slots能被用来接收signals,也就是signals connect到slots。也就是个函数,就像signals不知道是否有其他对象接收信号一样,Slots也不知道是否有signals会Connect到他
一个slots可以绑定到多个signals,也就是多个开关控制同一个灯泡,一个signals可以绑定到多个slots,也就是同一个开关可以控制多个灯泡
存在很多事先定义好的signals和slots,QAbstractButton存在clicked的signals,QLineEdit有一个名叫clear()的slot。可以将clicked信号绑定到clear的slot
connect() returns a QMetaObject.Connection object, which can be used with the disconnect() method to sever the connection.
signals也可以绑定到自由函数,不需要使用装饰器slot
The Signal Class
QtCore.Signal()用来声明类级别的信号signals,提交点击信号 clicked()
from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import QWidget
class Button(QWidget):
clicked = Signal(Qt.MouseButton)
def mousePressEvent(self, event):
self.clicked.emit(event.button())
The constructor of Signal takes a tuple or a list of Python types and C types:
signal1 = Signal(int) # Python types
signal2 = Signal(QUrl) # Qt Types
signal3 = Signal(int, str, int) # more than one type
signal4 = Signal((float,), (QDate,)) # optional types
Signal接受name参数,可用来代替变量
signal5 = Signal(int, name='rangeChanged')
rangeChanged.emit(...)
Signal还接受arguments参数,用在QML应用的,用name来指代被提交的变量
The Slot Class
装饰器 @QtCore.Slot(),在Qobject派生的类中指示Slots。Slot会接受参数name和result,result指定返回的类型,name和slot相同。信号signal绑定的函数最好用Slot来装饰,QML注册的QObject类更重要,缺少slot装饰器会触发bug。
通过设定环境变量export QT_LOGGING_RULES=“qt.pyside.libpyside.warning=true”,来检测是否被Slot装饰。
最好不要重载signals和slot,最好不要使用方法签名字符串来指定Signals和Slots
Slots函数应该使用@Slot来装饰
4 Creating a Dialog Application
使用基础的部件来构造一个对话框,用编辑区QLineEdit提供姓名,点击按钮显示欢迎句子QPushButton
可以使用PySide6 widgets组件来派生子类,QT提供了布局支持,用来组织不同的组件布局,使用QLineEdit.text()来访问编辑区的文本
# https://doc.qt.io/qtforpython-6/tutorials/basictutorial/dialog.html
import sys
from PySide6.QtWidgets import (QLineEdit, QPushButton, QApplication,
QVBoxLayout, QDialog)
from PySide6.QtCore import Slot
class Form(QDialog): # 派生类
def __init__(self, parent=None):
super(