我正在使用USB条形码扫描仪来设置Qt lineEdit
字段的文本,其文本随后用于GUI的其他功能(具体来说,文本是示例的名称)目前由用户测量,稍后将保存为文件名)。
我的问题是,我想动态覆盖lineEdit
字段中的当前文字,其中下一次扫描barcode
, ,用户不得不删除当前文字扫描前的手。因为我只是将扫描仪用作键盘模拟器,而不是从中正确读取串行信息,所以用户必须在扫描前单击文本字段。
我无法确定允许以下内容的lineEdit
连接操作:
from PyQt4 import QtGui
# add widgets etc
# ...........
# lineEdit part
self.mylineEdit = QtGui.QLineEdit()
#initialise to empty string on start up
self.mylineEdit.setText(' ')
#barcode scans here and then a returnPressed is registered
#connect to a function
self.mylineEdit.returnPressed.connect(self.set_sample_name) #here is where I want to delete the previous entry without backspacing by hand
#set the sample name variable
def set_sample_name(self):
self.sample_name = self.mylindEdit.text()
我想知道有没有办法在扫描下一个barcode
之前删除文本框中的上一个字符串? (文本字段暂时没有空白)..
感谢。
PS - 在Ubuntu 16.04上使用python3.5.2和pyQT4
答案 0 :(得分:1)
from PyQt5 import QtWidgets,QtCore
import sys
import os
class window(QtWidgets.QMainWindow):
def __init__(self):
super(window,self).__init__()
self.mylineEdit = QtWidgets.QLineEdit()
self.mylineEdit2 = QtWidgets.QLineEdit()
self.startNew=1
#initialise to empty string on start up
self.mylineEdit.setText(' ')
#barcode scans here and then a returnPressed is registered
#connect to a function
self.mylineEdit.returnPressed.connect(self.set_sample_name) #here is where I want to delete the previous entry without backspacing by hand
self.mylineEdit.textChanged.connect(self.delete_previous)
centwid=QtWidgets.QWidget()
lay=QtWidgets.QVBoxLayout()
lay.addWidget(self.mylineEdit)
lay.addWidget(self.mylineEdit2)
centwid.setLayout(lay)
self.setCentralWidget(centwid)
self.show()
#set the sample name variable
def set_sample_name(self):
self.sample_name = self.mylineEdit.text()
print(self.sample_name)
self.startNew=1
def delete_previous(self,text):
if self.startNew:
self.mylineEdit.setText(text[-1])
self.startNew=0
app=QtWidgets.QApplication(sys.argv)
ex=window()
sys.exit(app.exec_())
执行返回按下信号后,您可以更改标记self.startNew=1
,这将确保文本何时更改它将检查标记并在输入新barcode
后立即删除完整字符串。我在PyQt5中做过,但概念将保持不变。
该功能在self.myLineEdit
。