Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Update sql data models
Forum Update on Monday, May 27th 2025

Update sql data models

Scheduled Pinned Locked Moved Solved QML and Qt Quick
16 Posts 2 Posters 4.7k Views
  • 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.
  • D Diarby
    15 Jan 2018, 08:47

    @SGaist
    Hi. Yes I do.

    int RegionModel::primaryKeyId(const int row) {
    if (row >= 0 && row < rowCount()) {
    QSqlRecord record = this->record(row);
    return record.value("regionid").toInt();
    }
    return 0;
    }
    int SubRegionModel::primaryKeyId(const int row) {
    if (row >= 0 && row < rowCount()) {
    QSqlRecord record = this->record(row);
    return record.value("subregionid").toInt();
    }
    return 0;
    }

    void SubRegionModel::refresh(const int parentId) {
    setFilter(tr("regionid = %1").arg(parentId));
    }

    int SubZoneModel::primaryKeyId(const int row) {
    if (row >= 0 && row < rowCount()) {
    QSqlRecord record = this->record(row);
    return record.value("subzoneid").toInt();
    }
    return 0;
    }

    void SubZoneModel::refresh(const int parentId) {
    setFilter(tr("subregionid = %1").arg(parentId));
    }

    There are the 3 tables :
    create table region (
    RegionId integer primary key,
    Region varchar(16) not null unique
    );

    create table subregion (
    RegionId integer not null,
    SubRegionId integer primary key,
    SubRegion varchar(32) not null unique
    );

    create table subzone (
    SubRegionId integer not null,
    SubZoneId integer primary key,
    SubZone varchar(32) not null unique
    );

    D Offline
    D Offline
    Diarby
    wrote on 15 Jan 2018, 09:00 last edited by
    #6

    Please find data for tables :
    insert into region values (1,'Monde');
    insert into region values (2,'Afrique');
    insert into region values (19,'Amériques');
    insert into region values (142,'Asie');
    insert into region values (150,'Europe');
    insert into region values (9,'Océanie');

    insert into subregion values (2,14,'Afrique orientale');
    insert into subregion values (2,17,'Afrique centrale');
    insert into subregion values (2,15,'Afrique septentrionale');
    insert into subregion values (2,18,'Afrique australe');
    insert into subregion values (2,11,'Afrique occidentale');
    insert into subregion values (19,419,'Amérique latine et Caraïbes');
    insert into subregion values (19,21,'Amérique septentrionale');
    insert into subregion values (142,143,'Asie centrale');
    insert into subregion values (142,30,'Asie orientale');
    insert into subregion values (142,34,'Asie méridionale');
    insert into subregion values (142,35,'Asie du Sud-Est');
    insert into subregion values (142,145,'Asie occidentale');
    insert into subregion values (150,151,'Europe orientale');
    insert into subregion values (150,154,'Europe septentrionale');
    insert into subregion values (150,39,'Europe méridionale');
    insert into subregion values (150,155,'Europe occidentale');
    insert into subregion values (9,53,'Australie et Nouvelle-Zélande');
    insert into subregion values (9,54,'Mélanésie');
    insert into subregion values (9,57,'Micronésie');
    insert into subregion values (9,61,'Polynésie');

    insert into subzone values (419,29,'Caraïbes');
    insert into subzone values (419,13,'Amérique centrale');
    insert into subzone values (419,5,'Amérique du Sud');

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Jan 2018, 22:27 last edited by
      #7

      @Diarby said in Update sql data models:

      setFilter(tr("subregionid = %1").arg(parentId));

      Why are you translating that string ?

      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
      • D Offline
        D Offline
        Diarby
        wrote on 18 Jan 2018, 08:33 last edited by
        #8

        Hello,
        Excuse me for late answering. It's not a translation but a way to pass parentId parameter. Taking into account your remark, I changed to QString("subregionid = %1").arg(parentId), the result is the same : child combo are not updated.

        I am confused but this code is so simple that I wonder what is wrong ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 18 Jan 2018, 22:25 last edited by
          #9

          Did you check the content of the model after applying the filter on the C++ side ?

          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
          • D Offline
            D Offline
            Diarby
            wrote on 19 Jan 2018, 16:39 last edited by
            #10

            Hello,
            I did most checks in C++ and got the data for each parentId supplied as expected. But back to QML, the changes are not applied.
            In theory the QML combo, updates are done when the control is activated : I send the selected ID to child model so it can update his data and applied it to combo.
            Maybe I am wrong, but where ?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 19 Jan 2018, 21:19 last edited by
              #11

              Can you share your project ?

              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
              • D Offline
                D Offline
                Diarby
                wrote on 20 Jan 2018, 06:37 last edited by
                #12

                With pleasure. Whole project is compiled in world.7z (12 KiB) [0_1516430081478_world.7z](Uploading 100%)
                I receive message that I don't have privilèges to upload files. May I send you by email ?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 21 Jan 2018, 23:00 last edited by
                  #13

                  Not pre-compiled, just the sources.

                  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
                  • D Offline
                    D Offline
                    Diarby
                    wrote on 25 Jan 2018, 21:07 last edited by
                    #14

                    Hello ! I am happy to be back as politicians in my country blocked internet from saturday to wednesday. Please find here https://www.dropbox.com/s/6juslcf7dfkxk69/world.7z?dl=0 all sources of my project.
                    Thanks for your help.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 26 Jan 2018, 22:56 last edited by
                      #15

                      Your where close are wrong. For example, in SubRegionModel, it should be setFilter(QString("subregion.RegionId = %1").arg(parentId));.

                      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
                      • D Offline
                        D Offline
                        Diarby
                        wrote on 27 Jan 2018, 08:26 last edited by
                        #16

                        Hello, I made corrections and it works !
                        My mistake were to not set the table's name as the class is subclassed a QSqlRelationalTableModel.
                        Thanks a lot for your help.

                        1 Reply Last reply
                        0

                        15/16

                        26 Jan 2018, 22:56

                        • Login

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