PyQt 条形码扫描器 LineEdit

3
我正在使用USB条形码扫描器来设置Qt lineEdit字段的文本,该文本随后用于GUI的其他功能(具体而言,该文本是用户当前测量的样品名称,并将稍后保存为文件名)。
我的问题是,我想动态地用下一个扫描的barcode覆盖当前lineEdit字段中的文本,无需用户手动删除当前文本即可进行扫描。因为我只是使用扫描仪作为键盘仿真器,而不是从其中正确读取串行信息,所以用户必须在扫描之前单击文本字段。
我无法弄清楚哪个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()

我想知道在下一个条形码被扫描之前,是否有一种方法可以删除文本框中之前的字符串?(不使文本字段在一段时间内为空)。
谢谢。
PS-使用Python3.5.2和Ubuntu 16.04上的pyQT4。

你可以使用mylineEdit.setFocus()切换到lineEdit,并覆盖先前的字符串。我正在上传答案。 - Nimish Bansal
1个回答

2
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中实现。

太好了,这正是我所需要的,非常感谢你,Nimish! - raman_benny

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接