Results 1 to 3 of 3

Thread: Error Modifiying a record of a QSqlTableModel

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Error Modifiying a record of a QSqlTableModel

    Hi,

    I have a custom function inside a reimplementation of QSqlTableModel that intents to change a field value after the insertion. I insert the new record with:

    Qt Code:
    1. QSqlRecord newRecord;
    2. newRecord = mymodel->record();
    3. newRecord.setValue("CURRCODE","TestValue");
    4. mymodel->insertRecord(-1,newRecord);
    5. mymodel->updateNewRecord(); //Custom function
    To copy to clipboard, switch view to plain text mode 

    Inside the model, the custom function updateNewRecord has:
    Qt Code:
    1. qDebug() << "Previous Value: " << record(this->rowCount()-1).value("CURRCODE").toString(); //Prints "TestValue"
    2. record(this->rowCount()-1).setValue("CURRCODE","Newvalue"); //Setting a new value
    3. qDebug() << "New Value: " << record(this->rowCount()-1).value(codeColumn).toString(); //Prints "TestValue"!!!
    To copy to clipboard, switch view to plain text mode 

    The problem is that after setting the value with setValue()I still have the old value ("TestValue")!!!

    Any idea why?

    Thanks,
    Carlos.

  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: Error Modifiying a record of a QSqlTableModel

    QSqlQueryModel::record returns a copy of the record, so setValue on line 2 doesn't modify the record in the model
    A camel can go 14 days without drink,
    I can't!!!

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

    qlands (28th July 2011)

  4. #3
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Error Modifiying a record of a QSqlTableModel

    Yep... That was it!

    Thanks.
    Carlos.

Similar Threads

  1. Replies: 8
    Last Post: 30th March 2011, 20:06
  2. Replies: 7
    Last Post: 27th November 2010, 15:55
  3. Replies: 1
    Last Post: 4th October 2010, 00:46
  4. Replies: 3
    Last Post: 26th March 2010, 04:32
  5. deleting record (row) from QSqlTableModel
    By schnitzel in forum Qt Programming
    Replies: 3
    Last Post: 13th February 2010, 21:48

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.