Results 1 to 6 of 6

Thread: [Qt] TableView and QSqlQueryModel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2009
    Posts
    23
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [Qt] TableView and QSqlQueryModel

    Hello,
    It's me again ^^ and of course I have a problem. So.. I don't know how to delete records from tableView and QSqlQueryModel. I know how get number of row which is seleted but I don't know what do after. All is ok but if I want to delete a record from tableView (and from database) then I don't know how can I do it. Now I have written add button which add new record and refresh table but I don't know how to write delete button which delete selected row.

    tableView looks like:

    Header: ID | NAME
    1 | 23 | ASDF
    2 | 52 | FDGDR

    So I want to get value from column ID or something to delete row from database.

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Qt] TableView and QSqlQueryModel

    QSqlQueryModel is read only. You can't do dml operations with it.
    Use QSqlTableModel instead.

  3. #3
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt] TableView and QSqlQueryModel

    You can run a sql delete query to delete the underlying records in the database and then refresh your model.

  4. #4
    Join Date
    Dec 2009
    Posts
    23
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt] TableView and QSqlQueryModel

    @JD2000 yes I have this idea too but I don't know how to get value of currently selected item

  5. #5
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt] TableView and QSqlQueryModel

    The sql delete command has much the same syntax as a select query.

    Assuming that your table has a primary key (ID) as the first element on each row, you could try something like

    Qt Code:
    1. QString ID = model->data(model->index(TableView->selectionModel()->currentIndex().row(),0)).toString();
    2. QSqlQuery query;
    3. query.prepare("SELECT FROM table WHERE id = ?"); // or SELECT * FROM
    4. query.addBindValue(ID);
    5. query.exec();
    To copy to clipboard, switch view to plain text mode 

    Once you are happy that the correct row is being selected for deletion, substitute DELETE for SELECT in the above query. Please note I have not tested this but in theory it should work.

    There is probably a more elegant way to do this, I'll leave that to you!

  6. The following 2 users say thank you to JD2000 for this useful post:

    Raycho Raykov (4th February 2014), Xandareva (7th April 2010)

  7. #6
    Join Date
    Dec 2009
    Posts
    23
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt] TableView and QSqlQueryModel

    thx it works ;-)

Similar Threads

  1. Does 'QSqlQueryModel' have a bug?
    By nuntawat in forum Qt Programming
    Replies: 8
    Last Post: 6th April 2010, 17:45
  2. About QSqlQueryModel
    By vinny gracindo in forum Newbie
    Replies: 1
    Last Post: 11th December 2009, 22:27
  3. QComboBox QSqlQueryModel
    By aekilic in forum Qt Programming
    Replies: 8
    Last Post: 17th December 2008, 12:01
  4. tableview
    By GuL in forum Newbie
    Replies: 1
    Last Post: 26th August 2008, 17:18
  5. help in tableview
    By bala in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2007, 15:46

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.