未启用时将 QDoubleSpinBox 值设置为空字符串
Set QDoubleSpinBox value to an empty string, when not enabled
如果我有一个 QDoubleSpinBox 并且有一个名为 setEnabled
的可用方法,它采用布尔值。如果此值设置为 False,QDoubleSpinBox 将显示为灰色,显示微调按钮内的数字,但不允许用户键入任何内容或与之交互。我想要稍微修改一下,以便在未启用 QDoubleSpinBox 时,我希望将 Spin Button 值设置为空字符串或不呈现,并删除显示的任何先前值。
一种可能的解决方案是显示和隐藏 QLineEdit
of the QDoubleSpinBox
if the QDoubleSpinBox
is enabled or disabled, respectively. To detect the change of state of the enabled property, you must override the changeEvent()
method and verify the QEvent::EnabledChange
事件:
import sys
from PyQt5 import QtCore, QtWidgets
class DoubleSpinBox(QtWidgets.QDoubleSpinBox):
def changeEvent(self, e):
if e.type() == QtCore.QEvent.EnabledChange:
self.lineEdit().setVisible(self.isEnabled())
return super().changeEvent(e)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
radiobutton = QtWidgets.QRadioButton("enabled/disabled")
spinbox = DoubleSpinBox()
radiobutton.toggled.connect(spinbox.setDisabled)
w = QtWidgets.QWidget()
lay = QtWidgets.QVBoxLayout(w)
lay.addWidget(radiobutton)
lay.addWidget(spinbox)
w.show()
sys.exit(app.exec_())
如果我有一个 QDoubleSpinBox 并且有一个名为 setEnabled
的可用方法,它采用布尔值。如果此值设置为 False,QDoubleSpinBox 将显示为灰色,显示微调按钮内的数字,但不允许用户键入任何内容或与之交互。我想要稍微修改一下,以便在未启用 QDoubleSpinBox 时,我希望将 Spin Button 值设置为空字符串或不呈现,并删除显示的任何先前值。
一种可能的解决方案是显示和隐藏 QLineEdit
of the QDoubleSpinBox
if the QDoubleSpinBox
is enabled or disabled, respectively. To detect the change of state of the enabled property, you must override the changeEvent()
method and verify the QEvent::EnabledChange
事件:
import sys
from PyQt5 import QtCore, QtWidgets
class DoubleSpinBox(QtWidgets.QDoubleSpinBox):
def changeEvent(self, e):
if e.type() == QtCore.QEvent.EnabledChange:
self.lineEdit().setVisible(self.isEnabled())
return super().changeEvent(e)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
radiobutton = QtWidgets.QRadioButton("enabled/disabled")
spinbox = DoubleSpinBox()
radiobutton.toggled.connect(spinbox.setDisabled)
w = QtWidgets.QWidget()
lay = QtWidgets.QVBoxLayout(w)
lay.addWidget(radiobutton)
lay.addWidget(spinbox)
w.show()
sys.exit(app.exec_())