如何在PyQt5中按回车键从QTableView获取数据
How to get data from QTableView by pressing Enter Key in PyQt5
我正在使用 PyQt5 制作应用程序并遇到了一些麻烦。我想通过按 Enter 键从 QTableView 中获取 select 数据,并将其显示在 QLineEdit 中。我已经用 doubleClicked 信号完成了这些事情,但我仍然想通过两种方式向 QLineEdit 显示数据,然后在按下 Enter 键或双击后立即关闭 QTableView 对话框。
这是我的代码:
import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets, uic
class 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_()
还有我的桂:
让我解释一下:当点击按钮时,它显示了一个数据table,然后我想通过按回车键在table中输入select数据, 之后它将显示数据到 QlineEdit 并关闭 table
逻辑是检测按键,例如使用eventFilter,然后获取被选元素的QModelIndex:
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)
我正在使用 PyQt5 制作应用程序并遇到了一些麻烦。我想通过按 Enter 键从 QTableView 中获取 select 数据,并将其显示在 QLineEdit 中。我已经用 doubleClicked 信号完成了这些事情,但我仍然想通过两种方式向 QLineEdit 显示数据,然后在按下 Enter 键或双击后立即关闭 QTableView 对话框。 这是我的代码:
import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets, uic
class 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_()
还有我的桂:
让我解释一下:当点击按钮时,它显示了一个数据table,然后我想通过按回车键在table中输入select数据, 之后它将显示数据到 QlineEdit 并关闭 table
逻辑是检测按键,例如使用eventFilter,然后获取被选元素的QModelIndex:
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)