详解QDoubleSpinBox

发布时间:2023-05-19

一、qdoublespinbox显示小数

使用 QDoubleSpinBox 可以很容易地让小数在 UI 界面上得到良好的展示效果,这个 spinbox 的最小、最大值都可以通过 setValue 函数来改变。

from PyQt5.QtWidgets import QApplication, QDoubleSpinBox
app = QApplication([])
spinbox = QDoubleSpinBox()
spinbox.setRange(0.0, 1.0)
spinbox.setValue(0.5)
spinbox.show()
app.exec_()

二、qdoublespinbox设置值

如果我们希望在特定场景下设置 spinbox 的初值,可以使用 setValue 函数,这个函数接受一个浮点数参数,作为 spinbox 的初始值。

spinbox = QDoubleSpinBox()
spinbox.setRange(0.0, 1.0)
spinbox.setValue(0.7)

三、qdoublespinbox 右侧零

在展示数字时,QDoubleSpinBox 默认会在小数点后加上零。如果我们想要去掉这个零,可以使用 setDecimals 函数设置 spinbox 小数点后占位符的个数为 0。

spinbox = QDoubleSpinBox()
spinbox.setDecimals(0)

四、qdoublespinbox 科学计数法

QDoubleSpinBox 默认展示的是普通的浮点数,但在一些场景下,更希望以科学计数法形式展示。

from PyQt5.QtCore import Qt
spinbox = QDoubleSpinBox()
spinbox.setRange(0.0, 1.0)
spinbox.setDecimals(2)
spinbox.setDisplayScientific(True)
spinbox.setPrefix("Scientific Prefix: ")
spinbox.setSuffix(" Suffix")
spinbox.setAlignment(Qt.AlignRight | Qt.AlignVCenter)

五、qdoublespinbox怎么更改单位

有些场景下,希望以不同的单位来展示数值,可以使用 setSuffixsetPrefix 函数设置展示数值前后缀。

spinbox = QDoubleSpinBox()
spinbox.setRange(0.0, 60.0)
spinbox.setSuffix(" min")
spinbox.setPrefix("Time: ")
spinbox.setValue(30.0)

六、qdoublespinbox输入密码才能更改

有些时候,我们希望对某些设置进行保护,只有在输入正确的密码之后才能修改。可以使用 QLineEdit 的输入框,将输入框和 QDoubleSpinBox 进行 connect。

from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QHBoxLayout, QDoubleSpinBox, QVBoxLayout, QWidget
app = QApplication([])
password_edit = QLineEdit()
password_edit.setEchoMode(QLineEdit.Password)
password_label = QLabel("Password:")
password_label.setBuddy(password_edit)
spinbox = QDoubleSpinBox()
spinbox.setRange(0.0, 100.0)
layout = QHBoxLayout()
layout.addWidget(password_label)
layout.addWidget(password_edit)
password_box = QWidget()
password_box.setLayout(layout)
v_layout = QVBoxLayout()
v_layout.addWidget(password_box)
v_layout.addWidget(spinbox)
widget = QWidget()
widget.setLayout(v_layout)
widget.show()
def check_password():
    if password_edit.text() == "password":
        spinbox.setReadOnly(False)
    else:
        spinbox.setReadOnly(True)
password_edit.textChanged.connect(check_password)
app.exec_()

七、qdoublespinbox键盘事件

可以使用 QKeyPressEvent 来捕获键盘事件,以实现一些自定义操作。

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QDoubleSpinBox
app = QApplication([])
spinbox = QDoubleSpinBox()
def keyPressEvent(event):
    if event.key() == Qt.Key_Escape:
        spinbox.setValue(spinbox.minimum())
    else:
        super().keyPressEvent(event)
spinbox.show()
app.exec_()

八、qdoublespinbox调节按钮去除

可以使用 setButtonSymbols 函数将 spinbox 调节按钮去掉。

from PyQt5.QtWidgets import QApplication, QDoubleSpinBox
app = QApplication([])
spinbox = QDoubleSpinBox()
spinbox.setRange(0.0, 100.0)
spinbox.setButtonSymbols(QDoubleSpinBox.NoButtons)
spinbox.show()
app.exec_()

九、qdoublespinbox设置值没反应

有时候,我们会遇到 spinbox 设置初始值,但界面上并没有更新的问题。这种情况一般是因为最小值和最大值的设置有误。

spinbox = QDoubleSpinBox()
spinbox.setRange(0.0, 0.1)
spinbox.setValue(0.05)

十、qdoublespinbox连接信号槽

可以将 spinbox 和其他的信号槽进行连接,比如将 spinbox 和其他的控制界面进行连接。

from PyQt5.QtWidgets import QApplication, QDoubleSpinBox, QVBoxLayout, QMainWindow
app = QApplication([])
window = QMainWindow()
central_widget = QWidget()
window.setCentralWidget(central_widget)
spinbox = QDoubleSpinBox()
spinbox.setRange(0.0, 1.0)
spinbox.setSingleStep(0.01)
layout = QVBoxLayout(central_widget)
layout.addWidget(spinbox)
def on_value_changed(value):
    print(value)
spinbox.valueChanged.connect(on_value_changed)
window.show()
app.exec_()