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. How to insert new record in a QSqlRelationalTableModel
Forum Updated to NodeBB v4.3 + New Features

How to insert new record in a QSqlRelationalTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 1.2k 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.
  • P Offline
    P Offline
    Panoss
    wrote on last edited by Panoss
    #1

    I have a form which opens a child form (addArticle).
    When the child opens, I want to:

    • insert a new record
    • map controls (a LineEdit & a Combo Box) to the model

    I 've done it as shown below, but the data I enter in the controls does not get saved to the database. (model 's Edit Strategy is OnFieldChange)
    What am I doing wrong?

    This is the child 's constructor:

    addArticle::addArticle(QSqlRelationalTableModel *model, int *Idx, QDataWidgetMapper *mapper, QWidget *parent) :
        QDialog(parent),
        ui(new Ui::addArticle){
        ui->setupUi(this);
    
        this->model = model;
    
        //add new Record ;
        QSqlRecord newRecord = model->record();
    
        // if successfull insertion
        if (model->insertRecord(-1, newRecord)) {
            mapper->toLast();
            qDebug() << "New record inserted";
        }else{ // if failed
            qDebug() << "New record NOT inserted!!!";
        }
    
        // bound field for combo is field 'position'
        ui->positionEdit->setModel(model->relationModel(*Idx));
    
        // display field for combo is field 'name'
        ui->positionEdit->setModelColumn(model->relationModel(*Idx)->fieldIndex("name"));
    
        // map controls to data
        mapper->addMapping(ui->nameEdit, model->fieldIndex("name"));
        mapper->addMapping(ui->positionEdit, *Idx);
        mapper->toLast();
    
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Is your mapper submit policy AutoSubmit ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Panoss
        wrote on last edited by
        #3

        Yes, it is: mapper->submitPolicy= QDataWidgetMapper::AutoSubmit

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What happens if you manually call submit on the mapper ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • P Offline
            P Offline
            Panoss
            wrote on last edited by Panoss
            #5

            The same: a new record is created but only the autoincremented field is filled, the others are empty, null.
            I tried mapper->submit() and model->submitAll().

            Maybe it's because mapping is happening in the constructor of the form? And the controls on the form have not yet been fully created?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Are you sur the mapper is valid ?
              Why is it not part of your dialog rather than passed around ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Panoss
                wrote on last edited by Panoss
                #7

                The mapper is created in the parent form.
                It 's passed to the child (addArticle).
                I want to use it in the child for inserting a new record.

                How can I check if it's valid?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Check the current index.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Panoss
                    wrote on last edited by
                    #9

                    mapper 's currentIndex works ok, so I guess the mapper is valid.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Your record is empty - so what's your db structure. Does it allow NULL values, has it a PK?

                      P 1 Reply Last reply
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        Your record is empty - so what's your db structure. Does it allow NULL values, has it a PK?

                        P Offline
                        P Offline
                        Panoss
                        wrote on last edited by Panoss
                        #11

                        The table does have a primary key.
                        The record gets inserted in the database, so I guess it allows NULL values.

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            gillranjeett
                            wrote on last edited by gillranjeett
                            #13

                            I want to use it in the child for inserting a new record. mx player for pc mxplayer

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              The logic should be the other way around.
                              Use the dialog to get the data from your application user. Then create the record based on the data you extract from the dialog.
                              This will also allow you to properly handle cancellation of the dialog. Otherwise you may end up with empty rows in the database that you will need to remove because you added it too early in your business logic.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              P 1 Reply Last reply
                              1
                              • SGaistS SGaist

                                The logic should be the other way around.
                                Use the dialog to get the data from your application user. Then create the record based on the data you extract from the dialog.
                                This will also allow you to properly handle cancellation of the dialog. Otherwise you may end up with empty rows in the database that you will need to remove because you added it too early in your business logic.

                                P Offline
                                P Offline
                                Panoss
                                wrote on last edited by
                                #15

                                @SGaist said in How to insert new record in a QSqlRelationalTableModel:

                                Use the dialog to get the data from your application user. Then create the record based on the data you extract from the dialog.

                                I 'll do it this way.

                                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