Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the selection model from QSqlTableModel

How to get the selection model from QSqlTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 660 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      I 1 Reply Last reply
      1
      • VRoninV VRonin

        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

        I Offline
        I Offline
        Infinity
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved