Results 1 to 2 of 2

Thread: Selecting new row in QTableView

  1. #1
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Selecting new row in QTableView

    Hello again, I would like to select a row in the a QTableView after inserting it to database using QSqlQuery. . I can see the added row in QTableView but my problem is I cannot find QModelIndex which corresponds to the new row in the model. And i use QSortFilterProxyModel between the model and the view.

    How can i find QModelIndex of the inserted row and select it in QTableView?

    Qt Code:
    1. modelt = new QSqlTableModel(this);
    2. modelt->setTable("sianimtable");modelt->setEditStrategy(QSqlTableModel::OnRowChange);
    3. modelt->setHeaderData(1, Qt::Horizontal, "Code");
    4. modelt->setHeaderData(3, Qt::Horizontal, "Name);
    5. modelt->select();
    6.  
    7. filterModel = new QSortFilterProxyModel;
    8. filterModel->setSourceModel(modelt);
    9. filterModel->setFilterKeyColumn(2);
    10.  
    11. QTableView* view = new QTableView;
    12. view->setModel(filterModel);
    13. view->resizeColumnsToContents();
    14. view->horizontalHeader()->setStretchLastSection(true);
    15. view->setSelectionBehavior(QAbstractItemView::SelectRows);
    16. view->setSelectionMode(QAbstractItemView::SingleSelection);
    17. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
    18. view->hideColumn(0);
    19. view->hideColumn(2);
    20. view->hideColumn(4);
    21. view->hideColumn(5);
    22. view->hideColumn(6);
    23. view->setSortingEnabled(true);
    24. view->sortByColumn(1, Qt::AscendingOrder);
    25. view->verticalHeader()->hide();
    26. view->setAutoScroll(true);
    To copy to clipboard, switch view to plain text mode 

    Inserting with:
    Qt Code:
    1. QSqlQuery *q = new QSqlQuery();
    2. q->prepare("INSERT INTO sianimtable (CIPHER,CIPHERKP,TITLE,MSGON,MSGOFF) VALUES(:le2,:le1,:le3,:le4,:le5);");
    3. q->bindValue(":le1",le1->text());
    4. q->bindValue(":le2",le2->text());
    5. q->bindValue(":le3",le3->text());
    6. q->bindValue(":le4",le4->text());
    7. q->bindValue(":le5",le5->text());
    8.  
    9. if(q->exec()==false)
    10. {
    11. QMessageBox *pmsg = new QMessageBox;
    12. pmsg->setText("Can't connect");
    13. pmsg->setInformativeText(q->lastError().text());
    14. pmsg->exec();
    15. }
    16. delete q;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Selecting new row in QTableView

    You need to ask the model for the index. It could be the last row in the table but it doesn't have to be. By the way, C++ is not Java, you don't need all those pointers everywhere (at least not with QMessageBox and QSqlQuery).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Selecting a QLine
    By Paul Drummond in forum Qt Programming
    Replies: 6
    Last Post: 2nd September 2015, 16:30
  2. QTableView problem - selecting multiple items
    By junhonguk in forum Qt Programming
    Replies: 2
    Last Post: 18th July 2012, 14:16
  3. Selecting QGraphicsItems
    By SixDegrees in forum Qt Programming
    Replies: 3
    Last Post: 20th September 2010, 12:55
  4. QTableView currentChanged <> selecting header
    By Everall in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2009, 08:24
  5. Selecting row in QTableView
    By teS in forum Newbie
    Replies: 1
    Last Post: 15th January 2006, 11:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.