第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 PyQt5 掃描條形碼

使用 PyQt5 掃描條形碼

POPMUISE 2023-04-25 16:04:12
我有一個(gè) USB 條形碼掃描儀,我正在連接到我的電腦。每次掃描條形碼時(shí),它都會(huì)像鍵盤一樣將數(shù)據(jù)輸入計(jì)算機(jī)。我的目標(biāo)是將數(shù)據(jù)輸入到 PyQT5 Table 小部件中。我創(chuàng)建了下表,并將項(xiàng)目掃描到其中。問(wèn)題是,當(dāng)我掃描一個(gè)項(xiàng)目時(shí),它會(huì)編輯第一個(gè)單元格,但光標(biāo)不會(huì)自動(dòng)移動(dòng)到下一行,因此我可以將一個(gè)新項(xiàng)目掃描到表格中。我必須單擊第二個(gè)單元格,然后掃描該項(xiàng)目。然后單擊第三個(gè)單元格并掃描該項(xiàng)目,依此類推。我想知道如何使它自動(dòng)化,以便在將項(xiàng)目掃描到第一個(gè)單元格后,它會(huì)自動(dòng)移動(dòng)到下一個(gè)單元格并等待掃描儀的輸入?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貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊

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


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_())


查看完整回答
反對(duì) 回復(fù) 2023-04-25
?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

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


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_())


查看完整回答
反對(duì) 回復(fù) 2023-04-25
  • 2 回答
  • 0 關(guān)注
  • 279 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)