1 回答
TA贡献1789条经验 获得超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)
添加回答
举报