Results 1 to 9 of 9

Thread: QStandardItemModel changes to QSqlTableModel

  1. #1
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default QStandardItemModel changes to QSqlTableModel

    Hello i have a problem i looked a lot but did not find a exact solution. I have a
    QStandardItemModel to which i am adding data which is present in the QVectors which is retrieved from the data base and i set this data Model into the tableView.

    This is how i am displaying the data from a data base to QTableView
    Qt Code:
    1. model1 = new QStandardItemModel();
    2. for(int i=0;i< temptagidnames.count(); i++)
    3. {
    4. QStandardItem *item = new QStandardItem(temptagidnames.at(i));
    5. model1->setItem(i, 0, item);
    6. }
    7. model1->setHeaderData(0, Qt::Horizontal, tr("TAG ID"));
    8. model1->setHeaderData(1, Qt::Horizontal, tr("Description"));
    9. tableView->setModel(model1);
    10. tableView->setAlternatingRowColors(true);
    11. tableView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    To copy to clipboard, switch view to plain text mode 

    I wrote the below code to update the changes made in the QTableView into the sqlite db.

    Qt Code:
    1. model1 = new QStandardItemModel();
    2. QSqlTableModel *model1;
    3. model1 = new QSqlTableModel;
    4.  
    5. model1->setTable("tempchnames");
    6. model1->setEditStrategy(QSqlTableModel::OnFieldChange);
    7. tableView->setColumnHidden(0, true);
    8.  
    9. for(int i=0;i< temptagidnames.count(); i++)
    10. {
    11. QStandardItem *item = new QStandardItem(temptagidnames.at(i));
    12. model1->setItem(i, 0, item);
    13. }
    14.  
    15. model1->setHeaderData(0, Qt::Horizontal, tr("TAG ID"));
    16. model1->setHeaderData(1, Qt::Horizontal, tr("Description"));
    17.  
    18. tableView->setModel(model1);
    19. tableView->setAlternatingRowColors(true);
    20. tableView->setSizePolicy(QSizePolicy::Expanding,
    To copy to clipboard, switch view to plain text mode 

    But i get errors like, I am not able to know how i can link QStandardItemModel Model and QSqlTableModel so that the changes i make in the QTableView are reflected in the data base immediately.
    error C2371: 'model1' : redefinition; different basic types
    error C2039: 'setTable' : is not a member of 'QStandardItemModel'
    i am stuck with this problem past 2 days,.
    Thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel changes to QSqlTableModel

    You have to delete the first two lines of code

    Qt Code:
    1. model1 = new QStandardItemModel();
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QStandardItemModel changes to QSqlTableModel

    hello thank you for the reply.. i tried it but i have one error. I checked in detailed description of QSqlTableModel there is no setItem.. i did not find any alternate for it.. what should i do i order to remove the error

    error C2039: 'setItem' : is not a member of 'QSqlTableModel'
    this is how i am setting the data into the model
    model1->setItem(i, 0, item);
    thank you


    Added after 29 minutes:


    hi should i use setRecord or setData to display the data i have in the model into QTableView.. ?
    Pls help me out .


    Thank you
    Last edited by nagabathula; 20th April 2011 at 08:43.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel changes to QSqlTableModel

    To use QSqlTableModel you need to provide the name of a database table.

    After that call QSqlTableModel::select
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QStandardItemModel changes to QSqlTableModel

    hello i did not understand exactly how to do that.. You you pls show me a small example on how to do it. ..


    thank you

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel changes to QSqlTableModel

    Here you find more information
    A camel can go 14 days without drink,
    I can't!!!

  7. #7
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QStandardItemModel changes to QSqlTableModel

    Hello thank you so much for the help that solved my problem. I got what i wanted..

    Regards

  8. #8
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QStandardItemModel changes to QSqlTableModel

    Hello i have small warning and a query error when i try to display the data in QTableView after i do the changes.. i was wondering where i was going wrong. ?

    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    i saw in the forum that i have to delete the db connection but how can i do it. should i do it in the constructor.

    thank you

  9. #9
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel changes to QSqlTableModel

    To solve your problem you have to

    1. Close database connection
    2. Delete Model object(s)
    3. Remove Database


    For Example

    Qt Code:
    1. QString connName;
    2. {
    3. QSqlDatabase db = model->database();
    4. connName = db.connectionName();
    5.  
    6. db.close (); // 1
    7.  
    8. delete model; // 2.
    9. model = 0;
    10. }
    11. QSqlDatabase::removeDatabase(connName);
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 8
    Last Post: 30th March 2011, 20:06
  2. using QStandardItemModel
    By GrahamLabdon in forum Newbie
    Replies: 1
    Last Post: 6th March 2011, 09:13
  3. QAbstractItemModel, QStandardItemModel
    By homerun4711 in forum Newbie
    Replies: 2
    Last Post: 16th December 2010, 09:18
  4. QStandardItemModel Help
    By frenk_castle in forum Newbie
    Replies: 1
    Last Post: 16th January 2010, 17:54
  5. Print QStandardItemModel
    By Mrdata in forum Newbie
    Replies: 1
    Last Post: 4th July 2007, 23:14

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.