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

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

如何在 PyQt5 中按回車鍵從 QTableView 獲取數(shù)據(jù)

如何在 PyQt5 中按回車鍵從 QTableView 獲取數(shù)據(jù)

郎朗坤 2022-09-27 09:41:43
我正在使用PyQt5制作一個應(yīng)用程序,并面臨一些麻煩。我想通過按 輸入 鍵從QTable視圖中選擇數(shù)據(jù),并將其顯示在QLine編輯中。我已經(jīng)用雙擊信號做了這些事情,但我仍然想通過兩種方式向QLineEdit顯示數(shù)據(jù),然后在按輸入鍵或雙擊后立即關(guān)閉QTableView對話框。這是我的代碼:import sys import osfrom PyQt5 import QtCore, QtGui, QtWidgets, uicclass Application(QtWidgets.QMainWindow):    def __init__(self):        super(Application, self).__init__()        self.mainwindow = uic.loadUi('test.ui', self)        self.mainwindow.pushButton.clicked.connect(self.table)    def table(self):        self.table = QtWidgets.QTableView()        data = [            [2, 3, 5],             [23, 4, 5],            [2, 6, 7],            [0, 3, 5]        ]        self.model = TableModel(data)        self.table.setModel(self.model)        self.table.doubleClicked.connect(self.on_click)        self.table.show()    def on_click(self, signal):        row = signal.row()  # RETRIEVES ROW OF CELL THAT WAS DOUBLE CLICKED        column = signal.column()  # RETRIEVES COLUMN OF CELL THAT WAS DOUBLE CLICKED        cell_dict = self.model.itemData(signal)  # RETURNS DICT VALUE OF SIGNAL        cell_value = cell_dict.get(0)  # RETRIEVE VALUE FROM DICT        index = signal.sibling(row, 0)        index_dict = self.model.itemData(index)        index_value = index_dict.get(0)        print(            'Row {}, Column {} clicked - value: {}\n'.format(row, column, cell_value))        self.mainwindow.lineEdit.setText('%s' %cell_value)class TableModel(QtCore.QAbstractTableModel):    def __init__(self, data):        super(TableModel, self).__init__()        self._data = data    def data(self, index, role):        if role == QtCore.Qt.DisplayRole:            return self._data[index.row()][index.column()]    def rowCount(self, index):        return len(self._data)    def columnCount(self, index):        return len(self._data[0])if __name__ == '__main__':    application = QtWidgets.QApplication(sys.argv)    window = Application()    window.show()    application.exec_()還有我的桂:讓我解釋一下:當(dāng)點(diǎn)擊按鈕時,它會顯示一個數(shù)據(jù)表,然后我想通過按Enter鍵選擇表中的數(shù)據(jù),之后它將顯示數(shù)據(jù)到QlineEdit并關(guān)閉表
查看完整描述

1 回答

?
拉丁的傳說

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個贊

其邏輯是檢測按鍵,例如使用事件過濾器,然后獲取所選元素的 QModel 索引:


class Application(QtWidgets.QMainWindow):

    def __init__(self):

        super(Application, self).__init__()

        uic.loadUi("test.ui", self)


        self.pushButton.clicked.connect(self.table)


        self.table = QtWidgets.QTableView()

        self.table.doubleClicked.connect(self.write_text)

        self.table.installEventFilter(self)


    def table(self):

        data = [[2, 3, 5], [23, 4, 5], [2, 6, 7], [0, 3, 5]]

        self.model = TableModel(data)

        self.table.setModel(self.model)

        self.table.show()


    def write_text(self, index):

        row, column, cell_value = index.row(), index.column(), index.data()

        print("Row {}, Column {} clicked - value: {}".format(row, column, cell_value))

        self.lineEdit.setText("%s" % cell_value)

        self.table.close()


    def eventFilter(self, obj, event):

        if obj is self.table and event.type() == QtCore.QEvent.KeyPress:

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

                indexes = self.table.selectedIndexes()

                if indexes:

                    self.write_text(indexes[0])

        return super(Application, self).eventFilter(obj, event)


查看完整回答
反對 回復(fù) 2022-09-27
  • 1 回答
  • 0 關(guān)注
  • 326 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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