How to get the selection model from QSqlTableModel
-
Does the QSqlTableModel know which row is selected by a QTableView. I have something like this:
QSqlTableModel *m_accountModel = new QSqlTableModel(this); model.setTable("account"); model.select(); ... m_ui->tableViewAccount->setModel(m_accountModel); m_ui->tableViewAccount->resizeColumnsToContents(); m_ui->tableViewAccount->horizontalHeader()->setStretchLastSection(true); m_ui->tableViewAccount->setSelectionBehavior(QAbstractItemView::SelectRows); m_ui->tableViewAccount->setSelectionMode(QAbstractItemView::SingleSelection); m_ui->tableViewAccount->setEditTriggers(QAbstractItemView::NoEditTriggers)
Now I would like to get the selection of m_ui->tableViewAccount out of the m_accountModel. At the moment I'm doing it like this:
void FormAccount::on_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) { Q_UNUSED(deselected) if (m_ui->tableViewAccount->selectionModel()->hasSelection()) { qDebug() << "selection:" << selected; }
Is it possible to get the selection directly from the model without using FormAccount::on_selectionChanged()? Or is there an easier and cleaner way to achive this.
-
The model doesn't know the selection (it's always useful to remember that 1 model can be attached to multiple views and they can have different selections so it can't be a property of the model).
You can get it from the view usingm_ui->tableViewAccount->selectionModel()->selectedIndexes()
without using the signal however if that's what you want -
The model doesn't know the selection (it's always useful to remember that 1 model can be attached to multiple views and they can have different selections so it can't be a property of the model).
You can get it from the view usingm_ui->tableViewAccount->selectionModel()->selectedIndexes()
without using the signal however if that's what you want