Results 1 to 5 of 5

Thread: QSqlTableModel

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QSqlTableModel

    Hi everybody,
    I am searching examples how to use insertRow(); removeRow(); and updateRowInTable();
    Can i find somewhere examples?
    for display a table i havent problem. But how to construct and use insertRow(); removeRow(); and updateRowInTable();

    Qt Code:
    1. QString table = ui.tabelle_le->text();
    2. model->setTable(table);
    3. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    4. model->select();
    5.  
    6.  
    7. ui.tableView->setModel(model);
    8. ui.tableView->show();
    To copy to clipboard, switch view to plain text mode 

    If somebody have some finish examples why not

    Thanks again
    Think DigitalGasoline

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

    Default Re: QSqlTableModel

    Hmm... what exactly you don't know? In case of SQL models, insertRow() does an INSERT call and removeRow() does a DELETE call. Guess what the third method does

  3. #3
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QSqlTableModel

    Hi wysota,
    I know what should do insertRow(); removeRow(); and updateRowInTable();
    But i dont know how to use it.

    With this code i am able to view the values of my table:
    Qt Code:
    1. QString table = ui.tabelle_le->text();
    2. model->setTable(table);
    3. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    4. model->select();
    5.  
    6.  
    7. ui.tableView->setModel(model);
    8. ui.tableView->show();
    To copy to clipboard, switch view to plain text mode 

    And now i would like to insert a new Row or update a value or delete a Row.
    And my problem is:
    How to use:
    bool QAbstractItemModel::insertRow ( int row, const QModelIndex & parent = QModelIndex() )
    bool QAbstractItemModel::removeRow ( int row, const QModelIndex & parent = QModelIndex() )
    I have not found any examples how to use it.
    In the passt i use this for create a new row:
    Qt Code:
    1. QSqlQuery insert("insert into.....");
    2. insert.exec()..
    3. ..
    To copy to clipboard, switch view to plain text mode 

    I would like to use :
    Qt Code:
    1. bool QAbstractItemModel::insertRow ( int row, const QModelIndex & parent = QModelIndex() )
    2. bool QAbstractItemModel::removeRow ( int row, const QModelIndex & parent = QModelIndex() )
    To copy to clipboard, switch view to plain text mode 
    Can somebody show me how to use it?
    Think DigitalGasoline

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

    Default Re: QSqlTableModel

    Qt Code:
    1. model->insertRow(-1, QModelIndex());
    2. model->setData(model->index(model->rowCount()-1, 0), "somedata");
    To copy to clipboard, switch view to plain text mode 

    But it's better to do it this way:
    Qt Code:
    1. QSqlRecord rec = model->record();
    2. rec.setValue("name", "John Doe");
    3. rec.setValue("age", 18);
    4. model->insertRecord(-1, rec);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QSqlTableModel

    Hi wysota
    It works
    Thanks to open my mind! I am allways happy when i have the possibility to stude some examples
    For insert new Values:
    Qt Code:
    1. QString table = ui.tabelle_le->text();
    2. model->setTable(table);
    3. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    4. model->select();
    5.  
    6. QSqlRecord rec = model->record();
    7. ui.tableView->setModel(model);
    8. ui.tableView->show();
    9.  
    10. //I have auto increment at this table, so i dont have to give the new id
    11.  
    12. rec.setValue("status", "test");
    13. model->insertRecord(-1, rec);
    To copy to clipboard, switch view to plain text mode 

    For Update a Value it works now like this:
    Qt Code:
    1. QString table = ui.tabelle_le->text();
    2. model->setTable(table);
    3. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    4. model->select();
    5. ui.tableView->setModel(model);
    6. ui.tableView->show();
    To copy to clipboard, switch view to plain text mode 
    Last edited by raphaelf; 4th March 2006 at 13:12.
    Think DigitalGasoline

Similar Threads

  1. Trouble with "INSERT" by QSqlTableModel
    By AD in forum Qt Programming
    Replies: 12
    Last Post: 10th November 2008, 08:21
  2. QSqlTableModel slow even with manual submit
    By pherthyl in forum Qt Programming
    Replies: 2
    Last Post: 15th October 2008, 20:40
  3. QSqlTableModel inserts empty rows
    By Nesbitt in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 12:47
  4. Replies: 4
    Last Post: 9th May 2008, 17:02
  5. QSqlTableModel Help pls
    By munna in forum Newbie
    Replies: 1
    Last Post: 26th January 2006, 07:58

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.