亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 PyQt5 掃描條形碼

使用 PyQt5 掃描條形碼

POPMUISE 2023-04-25 16:04:12
我有一個 USB 條形碼掃描儀,我正在連接到我的電腦。每次掃描條形碼時,它都會像鍵盤一樣將數據輸入計算機。我的目標是將數據輸入到 PyQT5 Table 小部件中。我創建了下表,并將項目掃描到其中。問題是,當我掃描一個項目時,它會編輯第一個單元格,但光標不會自動移動到下一行,因此我可以將一個新項目掃描到表格中。我必須單擊第二個單元格,然后掃描該項目。然后單擊第三個單元格并掃描該項目,依此類推。我想知道如何使它自動化,以便在將項目掃描到第一個單元格后,它會自動移動到下一個單元格并等待掃描儀的輸入?import sys from PyQt5.QtWidgets import *    #Main Window class App(QWidget):     def __init__(self):         super().__init__()         self.title = 'Specimen Dashboard'        self.setWindowTitle(self.title)            self.tableWidget = QTableWidget()         self.createTable()         self.tableWidget.itemChanged.connect(self.go_to_next_row)            self.layout = QVBoxLayout()         self.layout.addWidget(self.tableWidget)         self.setLayout(self.layout)         self.show()        def go_to_next_row(self):        #Not working        #Trying to see if I can automatically move to next cell, but editing it         self.tableWidget.setItem(1,0, QTableWidgetItem("Name"))     #Create table     def createTable(self):           self.tableWidget.setRowCount(4)          self.tableWidget.setColumnCount(2)           self.tableWidget.horizontalHeader().setStretchLastSection(True)         self.tableWidget.horizontalHeader().setSectionResizeMode(             QHeaderView.Stretch) app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) 
查看完整描述

2 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

默認情況下,掃描儀發送一個結束行(“\n”),它被翻譯成 Return 或 Enter 鍵,這默認關閉編輯器,在這種情況下必須攔截該事件,移動光標并打開編輯器:


import sys


from PyQt5 import QtCore, QtWidgets



class TableWidget(QtWidgets.QTableWidget):

    def keyPressEvent(self, event):

        if (

            event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return)

            and self.state() == QtWidgets.QAbstractItemView.EditingState

        ):

            index = self.moveCursor(

                QtWidgets.QAbstractItemView.MoveNext, QtCore.Qt.NoModifier

            )

            self.selectionModel().setCurrentIndex(

                index, QtCore.QItemSelectionModel.ClearAndSelect

            )

            self.edit(index)

        else:

            super().keyPressEvent(event)



class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):

        super().__init__(parent)


        self.tableWidget = TableWidget(4, 2)

        self.setCentralWidget(self.tableWidget)

        self.tableWidget.horizontalHeader().setStretchLastSection(True)

        self.tableWidget.horizontalHeader().setSectionResizeMode(

            QtWidgets.QHeaderView.Stretch

        )



if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)

    w = MainWindow()

    w.show()

    sys.exit(app.exec_())


查看完整回答
反對 回復 2023-04-25
?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

您可以將表子類化并覆蓋closeEditor()hint參數告訴視圖當編輯器關閉時應該發生什么;默認情況下,當按下Enter當前單元格數據時提交,但您可以像這樣覆蓋此行為:


from PyQt5 import QtGui, QtWidgets


class Table(QtWidgets.QTableView):

? ? # leave to False for the default behavior (the next cell is the one at the

? ? # right of the current, or the first of the next row; when set to True it

? ? # will always go to the next row, while keeping the same column

? ? useNextRow = False


? ? def closeEditor(self, editor, hint):

? ? ? ? if hint == QtWidgets.QAbstractItemDelegate.SubmitModelCache:

? ? ? ? ? ? if self.useNextRow:

? ? ? ? ? ? ? ? super().closeEditor(editor, hint)

? ? ? ? ? ? ? ? current = self.currentIndex()

? ? ? ? ? ? ? ? newIndex = current.sibling(current.row() + 1, current.column())

? ? ? ? ? ? ? ? if newIndex.isValid():

? ? ? ? ? ? ? ? ? ? self.setCurrentIndex(newIndex)

? ? ? ? ? ? ? ? ? ? self.edit(newIndex)

? ? ? ? ? ? ? ? return

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? hint = QtWidgets.QAbstractItemDelegate.EditNextItem

? ? ? ? super().closeEditor(editor, hint)


if __name__ == '__main__':

? ? import sys

? ? app = QtWidgets.QApplication(sys.argv)

? ? test = Table()

? ? test.show()

? ? model = QtGui.QStandardItemModel(10, 5)

? ? test.setModel(model)

? ? sys.exit(app.exec_())


查看完整回答
反對 回復 2023-04-25
  • 2 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號