Results 1 to 4 of 4

Thread: Selecting a row in the a tableview after inserting it.

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

    Default Selecting a row in the a tableview after inserting it.

    Hello, I would like to select a row in the a tableview after inserting it. I'm inserting the row using QSqlQuery. I want to select the inserted row in a QTableView which uses this model. I can see the added row in tableview 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.
    I know that i must use:
    Qt Code:
    1. view->selectionModel()->select(i, QItemSelectionModel::Select);
    To copy to clipboard, switch view to plain text mode 
    How can i find QModelIndex of the inserted row and select it in tableview?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Selecting a row in the a tableview after inserting it.

    How are you inserting the row in first place ??

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

    Default Re: Selecting a row in the a tableview after inserting it.

    using such code:
    Qt Code:
    1. void MainWindow::insertRow()
    2. {
    3. QSqlDatabase db=QSqlDatabase::database();
    4. QString sqlstr=QString("INSERT INTO sianimtable (CIPHER,CIPHERKP,TITLE,MSGON,MSGOFF) VALUES(\"%1\",\"%2\",\"%3\",\"%4\",\"%5\");")
    5. .arg(le2->text())
    6. .arg(le1->text())
    7. .arg(le3->text())
    8. .arg(le4->text())
    9. .arg(le5->text());
    10.  
    11.  
    12. QSqlQuery q(db);
    13. if(q.exec(sqlstr)==false)
    14. {
    15. QMessageBox *pmsg = new QMessageBox;
    16. pmsg->setText("Не могу соединиться с базой данных");
    17. pmsg->setInformativeText(q.lastError().text());
    18. pmsg->exec();
    19. return;
    20. }
    21. modelt->select();
    22. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Selecting a row in the a tableview after inserting it.

    a bit changed code, but still need to select somehow.
    Qt Code:
    1. void MainWindow::insertRow()
    2. {
    3. QSqlQuery *q = new QSqlQuery();
    4. q->prepare("INSERT INTO sianimtable (CIPHER,CIPHERKP,TITLE,MSGON,MSGOFF) VALUES(:le1,:le2,:le3,:le4,:le5);");
    5. q->bindValue(":le2",le1->text());
    6. q->bindValue(":le1",le2->text());
    7. q->bindValue(":le3",le3->text());
    8. q->bindValue(":le4",le4->text());
    9. q->bindValue(":le5",le5->text());
    10.  
    11. if(q->exec()==false)
    12. {
    13. QMessageBox *pmsg = new QMessageBox;
    14. pmsg->setText("Не могу соединиться с базой данных");
    15. pmsg->setInformativeText(q->lastError().text());
    16. pmsg->exec();
    17. }
    18. delete q;
    19. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Selecting a QLine
    By Paul Drummond in forum Qt Programming
    Replies: 6
    Last Post: 2nd September 2015, 16:30
  2. Selecting QGraphicsItems
    By SixDegrees in forum Qt Programming
    Replies: 3
    Last Post: 20th September 2010, 12:55
  3. QTreeWidgetItem, selecting and removing
    By Zergi in forum Newbie
    Replies: 4
    Last Post: 10th January 2008, 18:45
  4. Selecting tabbed QDockwidgets
    By trskel in forum Qt Programming
    Replies: 3
    Last Post: 25th September 2007, 17:22
  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.