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 change record's fields in beforeInsert()?
Forum Updated to NodeBB v4.3 + New Features

How to change record's fields in beforeInsert()?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 341 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    The docs say:

    void QSqlTableModel::beforeInsert(QSqlRecord &record)
    This signal is emitted by insertRowIntoTable() before a new row is inserted into the currently active database table. The values that are about to be inserted are stored in record and can be modified before they will be inserted.

    In my QSqlRelationalTableModel I added this code:

    connect(model, &QSqlRelationalTableModel::beforeInsert, this, [=](QSqlRecord &record)
    {
        record.setValue("idProgram", 123); // just as an example
        qDebug() << record;
    });
    

    when running I add a new row using:

    model->insertRow(model->rowCount());
    QModelIndex index = model->index(model->rowCount() - 1, 0);
    if (!index.isValid()) return;
    ui->tableView->setCurrentIndex(index);
    ui->tableView->edit(index);
    

    the debug output is correct:

    QSqlRecord(3)
     0: QSqlField("idProgram", int, tableName: "timelines", generated: yes, autoValue: false, readOnly: false) "123" 
     1: QSqlField("time", int, tableName: "timelines", generated: no, autoValue: false, readOnly: false) "0" 
     2: QSqlField("h", double, tableName: "timelines", generated: no, autoValue: false, readOnly: false) "0"
    

    but the table view shows 0 instead of 123:

    b96a4d17-c3a2-4467-9215-acabc6a53a81-image.png

    Is there anything I should do further in order to see the updated values in the cells?

    JonBJ 1 Reply Last reply
    0
    • M Offline
      M Offline
      Mark81
      wrote on last edited by
      #2

      Actually, hiding the column leads to the expected behavior: the correct value is stored into the db. I just wonder why I cannot see it leaving the column visible.

      JonBJ 1 Reply Last reply
      0
      • M Mark81

        The docs say:

        void QSqlTableModel::beforeInsert(QSqlRecord &record)
        This signal is emitted by insertRowIntoTable() before a new row is inserted into the currently active database table. The values that are about to be inserted are stored in record and can be modified before they will be inserted.

        In my QSqlRelationalTableModel I added this code:

        connect(model, &QSqlRelationalTableModel::beforeInsert, this, [=](QSqlRecord &record)
        {
            record.setValue("idProgram", 123); // just as an example
            qDebug() << record;
        });
        

        when running I add a new row using:

        model->insertRow(model->rowCount());
        QModelIndex index = model->index(model->rowCount() - 1, 0);
        if (!index.isValid()) return;
        ui->tableView->setCurrentIndex(index);
        ui->tableView->edit(index);
        

        the debug output is correct:

        QSqlRecord(3)
         0: QSqlField("idProgram", int, tableName: "timelines", generated: yes, autoValue: false, readOnly: false) "123" 
         1: QSqlField("time", int, tableName: "timelines", generated: no, autoValue: false, readOnly: false) "0" 
         2: QSqlField("h", double, tableName: "timelines", generated: no, autoValue: false, readOnly: false) "0"
        

        but the table view shows 0 instead of 123:

        b96a4d17-c3a2-4467-9215-acabc6a53a81-image.png

        Is there anything I should do further in order to see the updated values in the cells?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @Mark81
        I don't think it will make any difference, but for the sake of a two second change does it alter if you make your lambda use [&] instead of [=]?

        Does the behaviour alter if you use a QSqlTableModel instead of a QSqlRelationalTableModel?

        You could subclass and override bool QSqlTableModel::updateRowInTable(int row, const QSqlRecord &values). Presumably that shows the original, unchanged value?

        1 Reply Last reply
        0
        • M Mark81

          Actually, hiding the column leads to the expected behavior: the correct value is stored into the db. I just wonder why I cannot see it leaving the column visible.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @Mark81 said in How to change record's fields in beforeInsert()?:

          Actually, hiding the column leads to the expected behavior: the correct value is stored into the db. I just wonder why I cannot see it leaving the column visible.

          You posted this after my response. Presumably Qt isn't allowing correctly for changing the values during that slot? Do some sort of update() or emit dataChanged() where you alter it? Check whether it's the same issue for QSqlTableModel as via QSqlRelationalTableModel?

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

            See https://bugreports.qt.io/browse/QTBUG-134432 :(

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              See my comment in the bug report:
              "After a first investigation I would say it worked for Qt4 but was broken in Qt5 with 48c68b05465e488020e241dd66290777b0eeeb3d in 2011 which leads to the question if this feature should not be declared broken and removed in Qt7... noone needed it for 14 years."

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              M 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                See my comment in the bug report:
                "After a first investigation I would say it worked for Qt4 but was broken in Qt5 with 48c68b05465e488020e241dd66290777b0eeeb3d in 2011 which leads to the question if this feature should not be declared broken and removed in Qt7... noone needed it for 14 years."

                M Offline
                M Offline
                Mark81
                wrote on last edited by
                #7

                @Christian-Ehrlicher if this feature is something that nobody uses, how would one fill the columns with default values when creating a new one? I'm not talking about SQL default values, but run-time generated. In my tables I'd use them quite often to pre-fill the columns with data that might be changed by the user, but usually does not.

                Examples:

                • data/time
                • expected values for foreign keys
                • expected values by the context, etc...
                JonBJ 1 Reply Last reply
                0
                • M Mark81

                  @Christian-Ehrlicher if this feature is something that nobody uses, how would one fill the columns with default values when creating a new one? I'm not talking about SQL default values, but run-time generated. In my tables I'd use them quite often to pre-fill the columns with data that might be changed by the user, but usually does not.

                  Examples:

                  • data/time
                  • expected values for foreign keys
                  • expected values by the context, etc...
                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8

                  @Mark81
                  Can't you create an empty initial QSqlRecord(), fill certain columns with setValue(), present the record to the user for editing and then insertRecord()?

                  M 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Mark81
                    Can't you create an empty initial QSqlRecord(), fill certain columns with setValue(), present the record to the user for editing and then insertRecord()?

                    M Offline
                    M Offline
                    Mark81
                    wrote on last edited by
                    #9

                    @JonB said in How to change record's fields in beforeInsert()?:

                    @Mark81
                    Can't you create an empty initial QSqlRecord(), fill certain columns with setValue(), present the record to the user for editing and then insertRecord()?

                    As far as I understand, but please correct me if I'm wrong, your approach would work if you create something like a QDialog with the fields to be edited and submitted.

                    Can you use the same approach while editing the table directly, using the OnRowChange edit strategy?

                    In this case when you create a new row the record is already appended to the model (here the need of the beforeInsert). Then the user can edit it, but it is already inserted.

                    JonBJ 1 Reply Last reply
                    0
                    • M Mark81

                      @JonB said in How to change record's fields in beforeInsert()?:

                      @Mark81
                      Can't you create an empty initial QSqlRecord(), fill certain columns with setValue(), present the record to the user for editing and then insertRecord()?

                      As far as I understand, but please correct me if I'm wrong, your approach would work if you create something like a QDialog with the fields to be edited and submitted.

                      Can you use the same approach while editing the table directly, using the OnRowChange edit strategy?

                      In this case when you create a new row the record is already appended to the model (here the need of the beforeInsert). Then the user can edit it, but it is already inserted.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #10

                      @Mark81
                      I have not checked, but isn't the new row and its record only inserted into the in-memory model at this point, it has not yet been inserted into the backend database? You can still edit/change it in code, before the user performs any edits, and it is not committed until you move off that new row?

                      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