Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Problem with QSqlTableModel: code that worked with Qt 4 does not work with Qt 5!
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sfilippidis
    wrote on last edited by
    #1

    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!

    https://www.filippidis.name/

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sfilippidis
        wrote on last edited by
        #3

        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? :-)

        https://www.filippidis.name/

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jmelbye
          wrote on last edited by
          #4

          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();@

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sfilippidis
            wrote on last edited by
            #5

            [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!

            https://www.filippidis.name/

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pcheng
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sfilippidis
                wrote on last edited by
                #7

                [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.

                https://www.filippidis.name/

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pcheng
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved