QAbstractItemView::indexAt method is for your question. But I propose you to work with selection model instead of detecting clicks.
Find the index of a clicked field in QTableView
-
10-10-2022 - |
Question
In a QTableView, I need to find the index of the field where it is clicked. I have the following method where i want to paste the value from the clipboard by pressing "ctrl+v" in the field of the table that is clicked:
QShortcut(QKeySequence('Ctrl+v'),self).activated.connect(self._handlePaste)
# paste the value
def _handlePaste(self):
clipboard_text = QApplication.instance().clipboard().text()
#item = QTableWidgetItem()
#item.setText(clipboard_text)
NvmQtModel.setData(self, index, clipboard_text, Qt.DisplayRole)
print clipboard_tex
Update:
# paste the value
def _handlePaste(self):
clipboard_text = QApplication.instance().clipboard().text()
index = QItemSelectionModel.selectedIndexes()
NvmQtModel.setData(self, index, clipboard_text, Qt.DisplayRole)
Exception:
index = QItemSelectionModel.selectedIndexes()
TypeError: QItemSelectionModel.selectedIndexes(): first argument of unbound method must have type 'QItemSelectionModel'
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow