How to catch "change selected row" signal in a QTableView?
-
@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. -
@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.
-
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!!
-
What about using the selection model from the table view directly ?
-
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.
-
@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. -
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? -
@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);