How to catch "change selected row" signal in a QTableView?
-
wrote on 8 Jun 2024, 00:07 last edited by
In QTableWidget one can connect
&QTableWidget::cellChanged
to a slot you provide
How is this done for QTableView? -
In QTableWidget one can connect
&QTableWidget::cellChanged
to a slot you provide
How is this done for QTableView?wrote on 8 Jun 2024, 02:56 last edited by Pl45m4 6 Aug 2024, 02:57@jdent said in How to catch "change selected row" signal in a QTableView?:
How is this done for QTableView?
There is no such function, but you can implement a signal like this on your own.
Looking at the
QTableWidget
source wherecellChanged
is emitted:void QTableWidgetPrivate::_q_emitItemChanged(const QModelIndex &index) { Q_Q(QTableWidget); if (QTableWidgetItem *item = tableModel()->item(index)) emit q->itemChanged(item); emit q->cellChanged(index.row(), index.column()); }
void QTableWidgetPrivate::_q_emitItemChanged
emitscellChanged
and is a private (hidden) function used inQTableWidget
to make it work as we all know. But you can add this behaviour to your ownQTableView
subclass.void QTableWidgetPrivate::setup() { Q_Q(QTableWidget); // model signals QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), q, SLOT(_q_emitItemChanged(QModelIndex))); }
You see here, that this function from above is connected to the
dataChanged(index, index)
signal of the model during the initialization of theQTableWidget
and its private logic.model
is aQAbstractItemModel*
internally.
You can use your model, of course. -
In QTableWidget one can connect
&QTableWidget::cellChanged
to a slot you provide
How is this done for QTableView?@jdent hi,
Something is not clear between your title and post content. The title mentions selection but the post is about cellChanged which are about two different things.
If you are thinking about selection, then take a look at QItemSelectionModel.
-
wrote on 8 Jun 2024, 22:43 last edited by
to be clear I am going to copy my code:
VectorModel* model = new VectorModel{}; QSortFilterProxyModel* proxy = new QSortFilterProxyModel{ this }; proxy->setSourceModel(model); ui.tableView->setModel(proxy); ui.tableView->setSortingEnabled(true); QDataWidgetMapper* mapper = new QDataWidgetMapper{}; mapper->setModel(proxy); mapper->addMapping(ui.lineEdit, 0); mapper->addMapping(ui.lineEdit_2, 1); mapper->toFirst(); QItemSelectionModel* selection_model = new QItemSelectionModel{ proxy }; connect(selection_model, &QItemSelectionModel::currentRowChanged , this, &QtWidgetsApplication1::currentRowChanged);
But changing the selection in the QTableView does not trigger the currentRowChanged signal!!
-
Lifetime Qt Championwrote on 9 Jun 2024, 06:35 last edited by SGaist 6 Oct 2024, 06:35
What about using the selection model from the table view directly ?
-
wrote on 9 Jun 2024, 22:45 last edited by jdent 6 Sept 2024, 23:13
How can I use the selectionModel of my VectorModel? My model inherits from QAbstractTableModel but I don't see any selectionModel in QAbstractTableModel functions/properties...
-
Lifetime Qt Championwrote on 10 Jun 2024, 06:35 last edited by SGaist 6 Oct 2024, 06:36
Sorry, I miswrote my answer (fixed it by the way). The selection model is returned by the view you are using. See selectionModel.
I meant to say, use that one directly.
-
wrote on 10 Jun 2024, 08:55 last edited by JonB 6 Oct 2024, 08:56
@jdent
Your question and @SGaist's answer happen to relate to the reply I have just posted at https://forum.qt.io/topic/157167/what-should-i-do-to-use-treeview-childs-to-select-a-subset-of-data-to-show-in-a-tableview/2. As I wrote there, see e.g. See e.g. https://stackoverflow.com/questions/2062889/qtableview-what-signal-is-sent-when-user-selects-a-row-by-clicking-to-it for use ofselectionModel()
. Selection is handled by the view. -
wrote on 11 Jun 2024, 21:02 last edited by
I have access to selection Model as @SGaist recommended and I wrote:
QItemSelectionModel* selection_model = ui.tableView->selectionModel(); connect(selection_model, &QItemSelectionModel::currentRowChanged , this, &QtWidgetsApplication1::currentRowChanged);
But I have a mapper like this:
mapper = new QDataWidgetMapper{}; mapper->setModel(proxy); mapper->addMapping(ui.lineEdit, 0); mapper->addMapping(ui.lineEdit_2, 1); mapper->toFirst();
and I need the mapper to "move to the new selected row", something like mapper->to(n) where n is the new current selection
so that the form widgets (e.g. ui.lineEdit) are kept in sync with the table view selection but how? -
I have access to selection Model as @SGaist recommended and I wrote:
QItemSelectionModel* selection_model = ui.tableView->selectionModel(); connect(selection_model, &QItemSelectionModel::currentRowChanged , this, &QtWidgetsApplication1::currentRowChanged);
But I have a mapper like this:
mapper = new QDataWidgetMapper{}; mapper->setModel(proxy); mapper->addMapping(ui.lineEdit, 0); mapper->addMapping(ui.lineEdit_2, 1); mapper->toFirst();
and I need the mapper to "move to the new selected row", something like mapper->to(n) where n is the new current selection
so that the form widgets (e.g. ui.lineEdit) are kept in sync with the table view selection but how?wrote on 11 Jun 2024, 21:31 last edited by@jdent
Are you asking about void QDataWidgetMapper::setCurrentModelIndex(const QModelIndex &index)Sets the current index to the row of the index if the orientation is horizontal (the default), otherwise to the column of the index.
This convenience slot can be connected to the signal currentRowChanged()connect(myTableView->selectionModel(), &QItemSelectionModel::currentRowChanged, mapper, &QDataWidgetMapper::setCurrentModelIndex);
3/10