【问题标题】:pyqt barcode scanner lineEditpyqt条码扫描线编辑
【发布时间】:2018-01-18 23:44:25
【问题描述】:

我正在使用 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()

我想知道有没有办法在扫描下一个barcode 之前删除文本框中的前一个字符串? (文本字段暂时没有变空)..

谢谢。

PS - 在 Ubuntu 16.04 上使用 python3.5.2 和 pyQT4

【问题讨论】:

  • 您可以使用 mylineEdit.setFocus() 切换到 lineEdit 并覆盖我正在上传答案的上一个字符串

标签: python qt pyqt barcode-scanner


【解决方案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中实现。

【讨论】:

  • 太棒了,完全符合我的需要,非常感谢 Nimish!
猜你喜欢
  • 1970-01-01
  • 2022-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
相关资源
最近更新 更多