Results 1 to 4 of 4

Thread: QSqlQueryModel edit table & direct query to the database

  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default QSqlQueryModel edit table & direct query to the database

    Hello all!

    I've started developing my programs using MVC. I've:

    Qt Code:
    1. model = new QSqlQueryModel;
    2. model->setQuery("SELECT ID_t, ID_podmiot FROM m_transporty LIMIT 0 , 30");
    3. if(model->lastError().isValid())
    4. qDebug() << model->lastError();
    5.  
    6. ui->tableView->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    Works beautifully!

    Now - how do I now edit a data (eg in the "line" table)? How to call a cell editing (I click on it and nothing happens), and later as the values ​​(for example, when you click "save") write to the database?
    I know I can read all the data in the model, but probably not the point of MVC. Since I changed one record (row) is what I overexpose the entire model?


    Can I implement editing only in the model?
    How do I add or delete a record not only the model but also from the database?

    Thanks!

  2. #2
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQueryModel edit table & direct query to the database

    The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags().
    Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.
    See query model example

  3. #3
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQueryModel edit table & direct query to the database

    Thanks!
    That what I'm saing!


    Allmost

    I'ld like to excecute any query, so I've:
    Qt Code:
    1. model = new EditableSqlModel;
    2. model->setQuery("SELECT ID_t, ID_podmiot, ID_oddzial, Numer, Numer_zleceniodawcy FROM m_transporty LIMIT 0 , 30");
    To copy to clipboard, switch view to plain text mode 
    but in example there is:

    Qt Code:
    1. void EditableSqlModel::refresh()
    2. {
    3. setQuery("select * from person");
    4. setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    5. setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
    6. setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
    7. }
    To copy to clipboard, switch view to plain text mode 

    I'd like to change query in setQuery automatyicly. I've tried use to
    Qt Code:
    1. setQuery(this->query().executedQuery());
    To copy to clipboard, switch view to plain text mode 
    but it's empty
    Last edited by TomASS; 2nd October 2011 at 15:02.

  4. #4
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQueryModel edit table & direct query to the database

    Quote Originally Posted by TomASS View Post
    I'd like to change query in setQuery automatyicly.
    Can you be a little more explicit? If you need to change query in model view, you will always have to call
    Qt Code:
    1. QSqlQueryModel::setQuery
    To copy to clipboard, switch view to plain text mode 
    (and also to set header data in model)

  5. The following user says thank you to cincirin for this useful post:

    TomASS (3rd October 2011)

Similar Threads

  1. Edit table for my own table-widget
    By MasterMatrix in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2010, 08:05
  2. Replies: 5
    Last Post: 3rd April 2010, 05:07
  3. QSqlite database lock + Delegate + QSqlQueryModel
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 13th October 2009, 12:52
  4. Replies: 1
    Last Post: 26th March 2009, 16:25
  5. View/edit database in a table and in a form
    By igorko in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2006, 10:09

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.