Results 1 to 4 of 4

Thread: QSqlTableModel programmatic changes

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

    Default QSqlTableModel programmatic changes

    I suspect I am thinking about QSqlTableModels in an overly simplistic fashion. (I'm new to both C++ and QT.)

    I have a QSqlTableModel, filled with data from an imported CSV file, that I want to write to the database (SQLite).
    Before it gets finally written, I give the user a chance to look over what will be written. Then, when they hit IMPORT, I timestamp the records in the tablemodel (the import date is row 5 in the table), and submit them to the database.
    Only, my timestamp doesn't work. The records save OK, but without the timestamp.

    Here is the code I think is relevant. The model, which has been passed around as a pointer, has an OnManualSubmit edit strategy.

    bool BankDB::Import(QSqlTableModel *thisModel)
    {
    QString importDate = QDate::currentDate().toString("yyyy-MM-dd") + " " + QTime::currentTime().toString("hh:mm:ss");
    for (int counter; counter < thisModel->rowCount(); counter ++)
    thisModel->record(counter).setValue(5,importDate);
    thisModel->submitAll();
    }

    I also tried using thisModel.record(counter).field(5).setValue(import Date) but to no avail.
    I know I can timestamp the records at the time they are parsed from the CSV and added to the tablemodel, but that strikes me as inelegant. I want the stamp right at the last moment, before the submitAll.
    What I am missing? You can't just fiddle with tablemodels as if they were 2D arrays before you write them? Or you can't setValue with a QString? It all compiles and runs fine. Just not the way I want it
    Also, is there a nifty way to a change a field to the same value in a whole tablemodel, without looping through it one record at a time?

    thanks in advance

    John
    Learning, one problem at a time.

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

    Default Re: QSqlTableModel programmatic changes

    PS, I'd be happy to let SQLite do the timestamping for me, but I can only get it to stamp the date, not the time. It stamps with the correct date but the time set to 00:00:00. Am I doing something wrong there?

    the relevant table is created as below. You're looking at field 5, rDate.
    "CREATE TABLE Transactions ("
    "aName TEXT(20) NOT NULL DEFAULT NULL,"
    "tDate Date NOT NULL DEFAULT NULL,"
    "tAmount Float NOT NULL DEFAULT 0,"
    "tDescription TEXT(100) NOT NULL DEFAULT NULL,"
    "aBalance Float NOT NULL DEFAULT 0,"
    "rDate Date NOT NULL DEFAULT CURRENT_TIMESTAMP,"
    "PRIMARY KEY (aName,tDate,tAmount,tDescription,aBalance),"
    "UNIQUE(aName,tDate,tAmount,tDescription,aBala nce) ON CONFLICT IGNORE)";
    Learning, one problem at a time.

  3. #3
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel programmatic changes

    try putting
    Qt Code:
    1. timestamp DATE DEFAULT (datetime('now','localtime')),
    To copy to clipboard, switch view to plain text mode 

    in your sqlite schema
    Got to keep the loonies on the path ...

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

    Default Re: QSqlTableModel programmatic changes

    Thanks. In the end I wrote a trigger into the schema that operated as soon as a line was added.
    But still I wonder why my first approach didn't work. You can't change a value in a table model before you submit it? Or am I just going about it the wrong way? Do I have to abstract a record model first, or something, rather than accessing tablemodel.record().setvalue ?

    John
    Learning, one problem at a time.

Similar Threads

  1. QSqlTableModel
    By codeman in forum Qt Programming
    Replies: 7
    Last Post: 4th May 2009, 11:04
  2. QSqlTableModel and LIKE
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2008, 08:45
  3. QSqlTableModel Help pls
    By munna in forum Newbie
    Replies: 1
    Last Post: 7th March 2006, 19:40
  4. QSqlTableModel
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 12:35
  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
  •  
Qt is a trademark of The Qt Company.