控件间信号传递
效果如图,用户可以通过SpinBox输入数据,或者直接拖拽右边的Slider控件。
代码如下:
from PySide6.QtWidgets import QApplication, QWidget, QSpinBox, QSlider, QHBoxLayout
from PySide6.QtGui import QFont
from PySide6.QtCore import Qt
import sys
class Widget(QWidget):
def __init__(self):
super(Widget, self).__init__()
"""创建spin box,并设置其范围为 1-100"""
spin_box = QSpinBox()
spin_box.setRange(0, 100)
"""创建slider,设置其范围 1-100"""
slider = QSlider(Qt.Orientation.Horizontal) # 水平滚动条
slider.setRange(1, 100)
"""spin_box 值改变时,同时改变 slider的值。"""
spin_box.valueChanged.connect(slider.setValue)
"""slider 值改变时,同样改变 spin_box 的值。"""
slider.valueChanged.connect(spin_box.setValue)
main_lay = QHBoxLayout()
main_lay.addWidget(spin_box)
main_lay.addWidget(slider)
self.setLayout(main_lay)
self.setWindowTitle("输入1-100之间的整数:")
if __name__ == "__main__":
app = QApplication()
app.setFont(QFont("宋体",28))
w = Widget()
w.show()
sys.exit(app.exec())
QSpinBox
QSpinBox提供的信号如下:
class QSpinBox(PySide6.QtWidgets.QAbstractSpinBox):
textChanged : ClassVar[Signal] = ... # textChanged(QString)
valueChanged : ClassVar[Signal] = ... # valueChanged(int)
调用QSpinBox的setValue()方法可以设置其数值,函数原型如下:
def setValue(self, val: int) -> None: ...
QSlider
QSlider提供信号如下:
class QAbstractSlider(PySide6.QtWidgets.QWidget):
actionTriggered : ClassVar[Signal] = ... # actionTriggered(int)
rangeChanged : ClassVar[Signal] = ... # rangeChanged(int,int)
sliderMoved : ClassVar[Signal] = ... # sliderMoved(int)
sliderPressed : ClassVar[Signal] = ... # sliderPressed()
sliderReleased : ClassVar[Signal] = ... # sliderReleased()
valueChanged : ClassVar[Signal] = ... # valueChanged(int)
创建slider时,我们可以改变参数来创建竖直滚动条,如下:
slider = QSlider(Qt.Orientation.Vertical)