Important: Please read the Qt Code of Conduct -

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);

    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)
        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 using m_ui->tableViewAccount->selectionModel()->selectedIndexes() without using the signal however if that's what you want

  • @VRonin That makes perfect sense. :-) Thank you very much.

Log in to reply