Results 1 to 2 of 2

Thread: How to use setValue in QSqlTableModel?

  1. #1
    Join Date
    Jul 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X

    Default How to use setValue in QSqlTableModel?

    I am trying to set the value of a field in a record of a QSqlTableModel, which is called thisModel.
    Here is the code snippet and the debug output:

    code
    QSqlTableModel *thisModel = new QSqlTableModel;
    thisModel->setTable("Transactions");
    thisModel->setFilter("aName = '" + accountName + "' ORDER BY tDate, rowid");
    thisModel->select();
    qDebug() << thisModel->record(0).field("aName").value();
    qDebug() << thisModel->record(0).field("aName").isReadOnly();
    thisModel->record(0).setValue("aName", "Test");
    qDebug() << thisModel->record(0).field("aName").value().toString();


    debug output
    QVariant(QString, "Streamline")
    false
    QVariant(QString, "Streamline")

    As you can see, the field is not read-only, but setValue leaves the field unchanged. I tried using the QVariant syntax for setValue, and I tried record(0).field("aName).setValue(etc), but still I can't budge the original data. Indeed thisModel->record(0).field("aName").clear() leaves it unchanged.
    Obviously, I am thinking about this all wrong. How do I do it?

    thanks

    John
    Learning, one problem at a time.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to use setValue in QSqlTableModel?

    record() is returning a copy, so you have to locally store it and set it back to the model using QSqlTableModel::insertRecord().

Similar Threads

  1. qsettings setvalue has something unnecessary
    By yj_yulin in forum Qt Programming
    Replies: 2
    Last Post: 14th June 2010, 05:46
  2. Problem in setValue for QAccessibleWidget
    By Rakesh_Kumar in forum Qt Programming
    Replies: 0
    Last Post: 29th January 2009, 09:36
  3. QSqlRecord setValue doesn't set the value
    By sgmurphy19 in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2008, 20:20
  4. SetValue
    By phillip_Qt in forum Qt Programming
    Replies: 4
    Last Post: 3rd October 2007, 22:45
  5. QProgressDialog::setValue crash( Mac )
    By marcel in forum Qt Programming
    Replies: 7
    Last Post: 11th April 2007, 23:12

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.