pyqt条码扫描器行 [英] pyqt barcode scanner lineEdit
问题描述
我正在使用USB条形码扫描仪设置Qt lineEdit
字段的文本,然后将其文本用于GUI的其他功能(具体来说,该文本是当前正在使用的示例的名称)由用户测量,稍后将另存为文件名).
I'm using a USB-barcode scanner to set the text of a Qt lineEdit
field, the text of which is then used for other features of the GUI (specifically, the text is the name of the sample currently being measured by the user and will be saved as a filename later).
我的问题是我想用下一个扫描的barcode
动态地覆盖lineEdit
字段中的当前文本,而无需,用户不必在扫描前手动删除当前文本.因为我只是将扫描器用作键盘仿真器,而不是从扫描器中正确读取串行信息,所以用户必须在扫描之前单击文本字段.
My issue is that I want to dynamically overwrite the current text in the lineEdit
field with the next scanned barcode
, without the user having to delete the current text by hand before scanning. Because I am simply using the scanner as a keyboard emulator rather reading properly the serial info from it, the user has to click the text field before scanning.
我不知道哪个lineEdit
连接动作将允许以下操作:
I can't figure out which lineEdit
connect action that would allow the following:
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
之前删除文本框中的上一个字符串? (文本字段暂时不会为空).
I'm wondering is there a way of deleting the previous string in the text box before the next barcode
is scanned? (without the text field going empty for a time)..
谢谢.
PS-在Ubuntu 16.04上使用python3.5.2和pyQT4
PS - Using python3.5.2 and pyQT4 on Ubuntu 16.04
推荐答案
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
中实现.
As soon as return pressed signal is executed you can change the flag self.startNew=1
which will ensure whenever the text will change it will check the flag and remove the complete string as soon as new barcode
is entered. I have done in PyQt5 but the concept will remain the same.
The functionality is achieved in self.myLineEdit
.
这篇关于pyqt条码扫描器行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!