在PyQt4中输入每个数字后,如何在修改后的QSpinBox中移动光标?

2024-10-01 04:45:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个QSpinBox类,它被修改为显示3位数(包括零)。在

class ThreeDigitSpinBox(QtGui.QSpinBox):
    def __init__(self, *args):
        QtGui.QSpinBox.__init__(self, *args)

    def textFromValue(self, value):
        return "%03d" % value

我的问题是,每次我选择并删除小部件上的现有值,并开始键入时,光标位置都会扰乱我的键输入。在

假设我刚刚删除了QSpinBox中的所有内容。然后我打算输入值“314”。在

(让我用“|”来表示光标)

当我输入“3”时,方框显示“003”,光标就在第一个0之后0 | 03。在

当我继续输入第二个数字“1”时,数字调整框显示“103”,光标就在第二个数字之后10 | 3

由于光标的位置,我想要的数字“314”从未输入。相反,它在键入“314”的所有3位数字后显示“1043”。在

如何确保将光标放在正确的位置以便正确输入数字?在

我使用的是WindowsXPSP3和Python2.7.3(32位)


Tags: self键入returninitvaluedefargs数字
1条回答
网友
1楼 · 发布于 2024-10-01 04:45:05

似乎可以使用基类QAbstractSpinBoxkeyboardTracking的参数

如果设置为false,则在您按下enter键之前,它不会发出valueChanged信号,否则小部件将失去焦点。在

Qt文档中的说明:

If keyboard tracking is enabled (the default), the spinbox emits the valueChanged() signal while the new value is being entered from the keyboard.

E.g. when the user enters the value 600 by typing 6, 0, and 0, the spinbox emits 3 signals with the values 6, 60, and 600 respectively.

If keyboard tracking is disabled, the spinbox doesn't emit the valueChanged() signal while typing. It emits the signal later, when the return key is pressed, when keyboard focus is lost, or when other spinbox functionality is used, e.g. pressing an arrow key.

pyqt docs在这里还是很没用的。在

相关问题 更多 >