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. Edit QSqlQueryModel (Or something similar) dynamically

Edit QSqlQueryModel (Or something similar) dynamically

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 2.4k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I've currently got a small QSqlQueryModel that I'm using to display data from a database. I've looked at threads such as EditableQSqlQueryModels and such that allow the user to edit the data from in-app. However, is there anything out there for setting data dynamically before it shows in the model? I have a certain String in my database that I want to replace to display data before it is shown on the table.

    Thanks

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Looks like calling QSqlQuery and then populating a QStandardItemModel is what best suits you

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Just wondering what View I would use with this as well. I am using the QT creator just to lay it all out, unsure of what view/model to use though. I've tired QTreeView but get this -

        error: no matching function for call to 'QTreeView::setModel(QStandardItemModel&)'
        ui->roomList->setModel(model);

        Guessing I'm doing something wrong here?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Yes, you are passing the model by value rather than by pointer:

          ui->roomList->setModel(&model);

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Thank you very much. Compiles well though and followed the exemplar for how to use QStandardItemModel. I've tried setting my QTreeView modal to it, however still a blank table.

            Code so far:

            QStandardItemModel model(4, 4);
            for(int row = 0; row < 4; row++) {
            for(int col = 0; col < 4; col++) {
            QStandardItem* item = new QStandardItem(QString("row %0, col %1").arg(row).arg(col));
            model.setItem(row, col, item);
            }
            }

            ui->roomList->setModel(&model);
            
            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              since you are creating QStandardItemModel on the stack it probably ends up being deleted before the view is ever displayed

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Apologies but I am lost as to what to do now then (Complete novice).

                Are you saying it's an issue because I already have the tree view created in Qt Creator and I'm trying to add into it in C++?

                mrjjM 1 Reply Last reply
                0
                • ? A Former User

                  Apologies but I am lost as to what to do now then (Complete novice).

                  Are you saying it's an issue because I already have the tree view created in Qt Creator and I'm trying to add into it in C++?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @JPanda

                  Hi
                  Your QStandardItemModel must live long.
                  So you better new it or else it will run out of scope.

                  Example

                  void Test() {
                  QStandardItemModel model;
                  } // scope ends here and Model variable is deleted.
                  So you cannot give to some other object as its gone when Test() ends.

                  so
                  void Test() {
                  QStandardItemModel * model = new QStandardItemModel (this); // thx VRonin. Remember to use a parent so u dont leak.
                  }

                  Now it will not be deleted when Test() ends.

                  So while the ui->roomList have a long life, currently your model seems to die very shortly after being filled :)

                  ? 1 Reply Last reply
                  2
                  • mrjjM mrjj

                    @JPanda

                    Hi
                    Your QStandardItemModel must live long.
                    So you better new it or else it will run out of scope.

                    Example

                    void Test() {
                    QStandardItemModel model;
                    } // scope ends here and Model variable is deleted.
                    So you cannot give to some other object as its gone when Test() ends.

                    so
                    void Test() {
                    QStandardItemModel * model = new QStandardItemModel (this); // thx VRonin. Remember to use a parent so u dont leak.
                    }

                    Now it will not be deleted when Test() ends.

                    So while the ui->roomList have a long life, currently your model seems to die very shortly after being filled :)

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    @mrjj Fantastic it works fine thank. Thank you both very much.

                    Learning the basics of C++ and coming from Java, the scoping is a little more difficult to get my head around. Thank you both though once again

                    1 Reply Last reply
                    1

                    • Login

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