如何让 QIntValidator() 在空时调用 editingFinished?
How do I make QIntValidator() call editingFinished when empty?
我有一个带有 QIntValidator 的行编辑小部件:
number_textbox = QLineEdit()
number_textbox.setText("0")
number_textbox.setValidator(QIntValidator())
此外,我将文本框的旧值存储在字典中:
old_val[uid_key] = 0
我希望能够在文本框为空时,如果用户在框外单击或按回车键,将文本框中的文本替换为旧值:
number_textbox.editingFinished.connect(lambda: self.editing_finished_function(number_textbox, old_val[uid_key]))
def editing_finished_function(self, textbox, old_val):
if textbox.text() == '':
textbox.setText(old_val)
else:
old_val = textbox.text()
不幸的是,QIntValidator() 似乎将空值视为无效,并且从不触发我的函数。有什么方法可以满足我的要求吗?
来自文档:
无效QLineEdit::editingFinished()
This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable.
QIntValidator 似乎不认为空字符串有效。所以我建议您使用 QRegExpValidator
创建自己的验证器
下面是接受数字和空字符串的验证器
regexp = QtCore.QRegExp('(^[0-9]+$|^$)')
validator = QtGui.QRegExpValidator(regexp)
完整示例:
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class MainWidget(QtWidgets.QWidget):
old_val = {'val': '0'}
def __init__(self):
super(MainWidget, self).__init__()
self.setLayout(QtWidgets.QVBoxLayout())
self.setFocusPolicy(QtCore.Qt.ClickFocus)
regexp = QtCore.QRegExp('(^[0-9]+$|^$)')
validator = QtGui.QRegExpValidator(regexp)
number_textbox = QtWidgets.QLineEdit()
number_textbox.setText(self.old_val['val'])
number_textbox.setValidator(validator)
number_textbox.editingFinished.connect(lambda: self.editing_finished_function(number_textbox))
self.layout().addWidget(number_textbox)
def editing_finished_function(self, textbox):
print("FINISHED")
if textbox.text() == '':
textbox.setText(self.old_val['val'])
else:
self.old_val['val'] = textbox.text()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widget = MainWidget()
widget.show()
sys.exit(app.exec_())
我有一个带有 QIntValidator 的行编辑小部件:
number_textbox = QLineEdit()
number_textbox.setText("0")
number_textbox.setValidator(QIntValidator())
此外,我将文本框的旧值存储在字典中:
old_val[uid_key] = 0
我希望能够在文本框为空时,如果用户在框外单击或按回车键,将文本框中的文本替换为旧值:
number_textbox.editingFinished.connect(lambda: self.editing_finished_function(number_textbox, old_val[uid_key]))
def editing_finished_function(self, textbox, old_val):
if textbox.text() == '':
textbox.setText(old_val)
else:
old_val = textbox.text()
不幸的是,QIntValidator() 似乎将空值视为无效,并且从不触发我的函数。有什么方法可以满足我的要求吗?
来自文档:
无效QLineEdit::editingFinished()
This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable.
QIntValidator 似乎不认为空字符串有效。所以我建议您使用 QRegExpValidator
下面是接受数字和空字符串的验证器
regexp = QtCore.QRegExp('(^[0-9]+$|^$)')
validator = QtGui.QRegExpValidator(regexp)
完整示例:
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class MainWidget(QtWidgets.QWidget):
old_val = {'val': '0'}
def __init__(self):
super(MainWidget, self).__init__()
self.setLayout(QtWidgets.QVBoxLayout())
self.setFocusPolicy(QtCore.Qt.ClickFocus)
regexp = QtCore.QRegExp('(^[0-9]+$|^$)')
validator = QtGui.QRegExpValidator(regexp)
number_textbox = QtWidgets.QLineEdit()
number_textbox.setText(self.old_val['val'])
number_textbox.setValidator(validator)
number_textbox.editingFinished.connect(lambda: self.editing_finished_function(number_textbox))
self.layout().addWidget(number_textbox)
def editing_finished_function(self, textbox):
print("FINISHED")
if textbox.text() == '':
textbox.setText(self.old_val['val'])
else:
self.old_val['val'] = textbox.text()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widget = MainWidget()
widget.show()
sys.exit(app.exec_())