Capturing a click from a table
-
Hi,
I've been beating my head against a wall for days on this.
tl; dr: I need an example of how to capture the row and column when a cell is clicked.
I have a Python3 program that looks like the following diagram:
class monitorMainWindow(QtWidgets.QMainWindow) mainTable = QTableView() mainModel = QAbstractTableModel() self.cellDoubleClicked.connect(self.get_clicked_cell)
mainModel contains the table data.
The cellDoubleClicked.connect(() function has been tried in all of the class definitions, the inclusion in the mainModel object above is just for example.I'm trying to capture the row and column that is double-clicked but I cannot fathom the how of it.
I've tried connecting a clicked() event to various different classes but the program doesn't start, saying that clicked() is not a valid function for that class.
I've found references to cellDoubleClicke(d) as well but that fails with similar errors.
Can you please help? An example of how to capture the cell that was clicked would be extremely useful or even guidance on what I am missing would be appreciated.
-
@Parthe said in Capturing a click from a table:
cellDoubleClicked
There is no such signal, even less so in QMainWindow.
QTableView has this signal: https://doc.qt.io/qt-6/qabstractitemview.html#doubleClicked
Just check documentation. -
Thanks for the quick reply @jsulm, it is appreciated.
According to the same doc there is a cellDoubleClicked() signal for the QTableWidget class, here:
https://doc.qt.io/qt-6/qtablewidget.html#cellDoubleClickedand I thought that the QAbstractTableModel came from the QTableWidget, but I guess not.
I will try your doubleClicked() signal and report back.
-
Honestly @jsulm I don't know, it was just something that came up in my search for answers and it looked like it would work.
I am getting confused between QTableView, QTableWidget and QAbstractTableModel, but since I have got a QTabvleView object in my current program and you have kindly provided me with the answer I am going down that route :-)
-
@Parthe
QTableView
is just the view. You have to supply it with a model, either your own derived fromQAbstractItem
/TableModel
or one pre-supplied (e.g.QStandardItemModel
).QTableWidget
is aQTableView
with its own, private model already built into it, plus a few extras on the view side.QTableWidget
s are fine for playing. They cannot do anything which aQAbstractTableModel
+QTreeView
cannot be made to do. The latter are more flexible thanQTableWidget
, but that is simpler than the combination. -
@Parthe the doubleClicked signal @jsulm pointed you to is the correct answer. The parameter is the index corresponding to the cell you double clicked and that index will provide you with the row and column you are looking for.