[SOLVED] Problem with QSqlTableModel: code that worked with Qt 4 does not work with Qt 5!



  • I am having a problem with an application that uses an SQLite database. Previously (Qt4) it used to work without any problems, but with Qt5-RC2 with the same codebase (I only changed some #include statements: different names from Qt4 to Qt5) I seem to have a problem with

    @QDataWidgetMapper::submit()@

    The submit() statement seems to give a false value, where it was true in Qt4. Practically, I have a submission form and the submit button works fine with Qt4 and it does not work (does not write to the database) with Qt5-RC2. Any ideas? Are your Qt4 SQLite applications working with Qt5-RC2? I am using the provided precompiled library+Qt Creator (the respective dmg download) with a Mac (OS X 10.8.2).

    edit: the problem seems to be isolated (but unsolved) now: see my next post at this thread! Edited the title to reflect the new situation!



  • There were quite some "changes":https://qt.gitorious.org/qt/qtbase/blobs/stable/dist/changes-5.0.0#line758 to the QtSql module.



  • I am still having the problem I posted! To be more specific, the code

    @
    QSqlRecord record = model->record();
    bool isRecordCreated = model->insertRecord(-1,record);
    mapper->setCurrentIndex(model->rowCount()-1);
    @

    In Qt 4.7 (test also with 4.8) it used to work properly: I used this fragment of code in the case of the first record to be added at the table. Now (Qt 5.0.0) it doesn't work: qDebug says that isRecordCreated is false!

    Any help? :-)



  • Does your database table that is being modeled have any constraints (primary key, not null, etc.)? It looks like when you insert your record, you haven't put any values in it. Changing from Qt 4.8 to 5.0 shouldn't effect that, but let's rule it out.

    Does model->lastError() give any information after trying to insert the record?

    @bool isRecordCreated = model->insertRecord(-1,record);
    if (!isRecordCreated) qDebug() << model->lastError();@



  • [quote author="jmelbye" date="1358632565"]Does your database table that is being modeled have any constraints (primary key, not null, etc.)? It looks like when you insert your record, you haven't put any values in it. Changing from Qt 4.8 to 5.0 shouldn't effect that, but let's rule it out.

    Does model->lastError() give any information after trying to insert the record?

    @bool isRecordCreated = model->insertRecord(-1,record);
    if (!isRecordCreated) qDebug() << model->lastError();@[/quote]

    Thank you for your advice! Thanks to it, I solved the problem! Actually, it was working well with 4.7 and 4.8 but not with 5.0. The problem had to do with trying to write an empty record!



  • Stavro,

    I have the same problem with trying to write an empty record. Did you find a way around it?

    I want the user to click New and this will generate a new record for input. I am using QDataWidgetMapper to display record data.

    This is the code I have for the record:

    @
    QSqlRecord sqlRecord1;
    bool isRecordCreated = model->insertRecord(-1,sqlRecord1);
    if (!isRecordCreated)
    qDebug() << model->lastError();
    else
    {
    mapper->toLast();
    clearFields();
    }
    @

    Thanks,

    Pericles



  • [quote author="pcheng" date="1390389507"]Stavro,

    I have the same problem with trying to write an empty record. Did you find a way around it?

    I want the user to click New and this will generate a new record for input. I am using QDataWidgetMapper to display record data.

    This is the code I have for the record:

    @
    QSqlRecord sqlRecord1;
    bool isRecordCreated = model->insertRecord(-1,sqlRecord1);
    if (!isRecordCreated)
    qDebug() << model->lastError();
    else
    {
    mapper->toLast();
    clearFields();
    }
    @

    Thanks,

    Pericles[/quote]

    Pericles,

    My program was working well with 4.7 and 4.8 but not with 5.0. The problem had to do with trying to write an empty record! So, I solved it by rewriting the respective parts of the code. My application is free software ("GPLv3":http://www.gnu.org/licenses/gpl.html) so you have access to the respective source code (under the terms of "GPLv3":http://www.gnu.org/licenses/gpl.html). The project's webpage is "here":http://www.stavros.filippidis.name/english/free_software.html.



  • Thanks for the input.

    My program is a bit different in that it has a NEW button which moves you to an empty record rather than an Add which opens a new dialog that is empty.

    I finally found that instead of using model->insertRecord(); I can use model->insertRow(model->rowCount()); and this will insert a new row in the table with no problems with empty fields. Then I use mapper->toLast(); to move to that record.

    Pericles


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.