Results 1 to 3 of 3

Thread: QSqlRelationalTableModel doesn't edit all rows after filtering

  1. #1
    Join Date
    Dec 2008
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation QSqlRelationalTableModel doesn't edit all rows after filtering

    Hi all.
    The problem is at the bottom.
    First i create my tables:
    Qt Code:
    1. query.exec("CREATE TABLE grupi("
    2. "grupa_id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,"
    3. "grupa VARCHAR(10) NULL UNIQUE);");
    4. query.exec("CREATE TABLE contacts("
    5. "contact_id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,"
    6. "FirstName VARCHAR(30) NULL,"
    7. "LastName VARCHAR(30) NULL,"
    8. "grupaId INT NOT NULL,"
    9. " FOREIGN KEY (grupaId) REFERENCES grupi);");
    10. query.exec("insert into grupi values('VIP')");
    11. query.exec("insert into grupi values('Family')");
    12. query.exec("insert into grupi values('Ungrouped')");
    13. query.exec("insert into grupi values('Work')");
    To copy to clipboard, switch view to plain text mode 
    The model gets populated, and i can see all of the contacts in the database:
    Qt Code:
    1. dataManagementModel = new QSqlRelationalTableModel(this);
    2. dataManagementModel->setTable("contacts");
    3. dataManagementModel->setRelation(3, QSqlRelation("grupi", "grupa_id", "grupa"));
    4. dataManagementModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    5. bool ok1 = dataManagementModel->select();
    To copy to clipboard, switch view to plain text mode 
    The problem:
    After i have created the tables and populate them with some data, the problem is when i want to delete a group. If i want to delete a group, first the contacts that were in that group must be set to another group. Let say to 'Ungrouped'. The code for this action is:
    Qt Code:
    1. void DataManagement::setContactsToDefaultGroup(QModelIndex oldData, QModelIndex newData)
    2. {
    3. dataManagementModel->setFilter("grupaId = " + oldData.data().toString()); //to get only the contacts that are in the group that's going to be deleted
    4. bool ok = dataManagementModel->select();
    5. int rowsCounted = dataManagementModel->rowCount();
    6. for (int row = 0; row < rowsCounted; row++)
    7. {
    8. QModelIndex indexForUpdate;
    9. QSqlRecord rec = dataManagementModel->record(row);
    10. QString name = rec.value(1).toString(); //after some time it doesn't read the name
    11. indexForUpdate = dataManagementModel->index(row, 3, QModelIndex()); //gives invalid index after some time
    12. dataManagementModel->setData(indexForUpdate, newData.data().toInt());
    13. QSqlError h = dataManagementModel->query().lastError();
    14. bool b = dataManagementModel->submitAll();
    15. Q_ASSERT(b);
    16. }
    17. dataManagementModel->setFilter(""); //to show all the contacts again
    18. dataManagementModel->select();
    19. }
    To copy to clipboard, switch view to plain text mode 
    This function can read and edit the first 2-3 contacts and fails. Is dataManagementModel used properly? How to move contacts from one group to another group?


    As for convenience I have provided part of the code that you can build.
    Attached Files Attached Files
    Last edited by slobo_n; 24th March 2009 at 15:47. Reason: updated contents

  2. #2
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: QSqlRelationalTableModel doesn't edit all rows after filtering

    Hi,

    why are you not using a simple query to first update the group field (e.g. set it to "ungrouped), delete the group with another query and then call select on the model?

  3. The following user says thank you to janus for this useful post:

    slobo_n (24th March 2009)

  4. #3
    Join Date
    Dec 2008
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlRelationalTableModel doesn't edit all rows after filtering

    Thanks for the quick reply.
    Quote Originally Posted by janus View Post
    Hi,

    why are you not using a simple query to first update the group field (e.g. set it to "ungrouped), delete the group with another query and then call select on the model?
    I have tried and worked . Thanks a lot.

Similar Threads

  1. Multiple clicks required to get into edit mode on QTreeView
    By JonInAnnArbor in forum Qt Programming
    Replies: 10
    Last Post: 26th November 2012, 18:59
  2. Character encoding in text edit and llne edit
    By greenvirag in forum Qt Programming
    Replies: 3
    Last Post: 20th January 2009, 08:45
  3. Edit items in table model
    By gyre in forum Newbie
    Replies: 2
    Last Post: 14th January 2008, 03:08
  4. Line Edit Controll problem
    By mansoorulhaq in forum Qt Programming
    Replies: 5
    Last Post: 31st October 2007, 08:33
  5. Replies: 8
    Last Post: 15th May 2007, 09:21

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.