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. Using 2 different ListModels with 2 different ComboBoxes

Using 2 different ListModels with 2 different ComboBoxes

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
14 Posts 2 Posters 1.3k Views 2 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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #4

    I have no idea what your ListModel or PatientModel classes do, so I can't answer that.

    But if these 2 combo boxes need to display different data, then you definitely need 2 different models. Or at least 2 different instances of the same model with some different flags set so that they will know to serve different data.

    (Z(:^

    G 1 Reply Last reply
    0
    • sierdzioS sierdzio

      I have no idea what your ListModel or PatientModel classes do, so I can't answer that.

      But if these 2 combo boxes need to display different data, then you definitely need 2 different models. Or at least 2 different instances of the same model with some different flags set so that they will know to serve different data.

      G Offline
      G Offline
      gabor53
      wrote on last edited by gabor53
      #5

      @sierdzio
      Would you please show me an example how to create 2 different instances of the same ListModel to serve data from 2 different tables from the same database. I tried to give them different ids but, that clearly doesn't work.
      Thank you.

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #6

        @gabor53 said in Using 2 different ListModels with 2 different ComboBoxes:

        ListModel

        Wait wait wait, you are using stock ListModel? That is a static model, it does not connect to any database whatsoever.

        What you need is some subclass of QSqlQueryModel or QSqlTableModel (or custom QAbstractItemModel if you dare), set it up so that it gets correct data from DB, then expose it to QML and then it will work.

        Something like:

        class QueryModel: public QSqlQueryModel
        {
            Q_OBJECT
        public:
            explicit QueryModel(QObject *parent = nullptr) : QSqlQueryModel(parent)
          {
            setQuery("SELECT someColumn FROM SomeTable", QSqlDatabase::database("my_db"));
          }
        };
        
        //...
        qmlEngine->rootContext()->setContextProperty("myQueryModel", queryModel);
        
        // QML
        model: myQueryModel
        
        

        (Z(:^

        G 1 Reply Last reply
        0
        • sierdzioS sierdzio

          @gabor53 said in Using 2 different ListModels with 2 different ComboBoxes:

          ListModel

          Wait wait wait, you are using stock ListModel? That is a static model, it does not connect to any database whatsoever.

          What you need is some subclass of QSqlQueryModel or QSqlTableModel (or custom QAbstractItemModel if you dare), set it up so that it gets correct data from DB, then expose it to QML and then it will work.

          Something like:

          class QueryModel: public QSqlQueryModel
          {
              Q_OBJECT
          public:
              explicit QueryModel(QObject *parent = nullptr) : QSqlQueryModel(parent)
            {
              setQuery("SELECT someColumn FROM SomeTable", QSqlDatabase::database("my_db"));
            }
          };
          
          //...
          qmlEngine->rootContext()->setContextProperty("myQueryModel", queryModel);
          
          // QML
          model: myQueryModel
          
          
          G Offline
          G Offline
          gabor53
          wrote on last edited by
          #7

          @sierdzio
          I used this to connect the db to the model:

          //Read data from MedNamesTable
          function dbGetMeds() {
              console.log("Entered dbGetMeds.")
              var db = dbGetHandle()
              db.transaction(function (tx) {
                  var results = tx.executeSql(
                              "SELECT MednameField FROM MedNamesTable order by MednameField asc")
                  for (var i = 0; i < results.rows.length; i++) {
                      listModel2.append({
                                            "MednameField": results.rows.item(
                                                                i).MednameField,
                                            "checked": ""
                                        })
                  }
              })
          }
          

          It works in the first ComboBox but not in the second.

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #8

            Ah ok that makes it clear.

            The reason why it does not work is that you only modify listModel or listModel2 in your DB functions. If you want the data to be present in both objects, you need to modify both of them, so something like:

            let obj = {
              "MednameField": results.rows.item(i).MednameField,
              "checked": ""
            }
            
            listModel.append(obj)
            listModel2.append(obj)
            

            I still would say it's better to use the c++ classes for this purpose, though. Hacking ListModel seems like a temporary solution at best.

            (Z(:^

            G 2 Replies Last reply
            1
            • sierdzioS sierdzio

              Ah ok that makes it clear.

              The reason why it does not work is that you only modify listModel or listModel2 in your DB functions. If you want the data to be present in both objects, you need to modify both of them, so something like:

              let obj = {
                "MednameField": results.rows.item(i).MednameField,
                "checked": ""
              }
              
              listModel.append(obj)
              listModel2.append(obj)
              

              I still would say it's better to use the c++ classes for this purpose, though. Hacking ListModel seems like a temporary solution at best.

              G Offline
              G Offline
              gabor53
              wrote on last edited by
              #9

              @sierdzio
              No I understand. Thank you for your help.

              1 Reply Last reply
              0
              • sierdzioS sierdzio

                Ah ok that makes it clear.

                The reason why it does not work is that you only modify listModel or listModel2 in your DB functions. If you want the data to be present in both objects, you need to modify both of them, so something like:

                let obj = {
                  "MednameField": results.rows.item(i).MednameField,
                  "checked": ""
                }
                
                listModel.append(obj)
                listModel2.append(obj)
                

                I still would say it's better to use the c++ classes for this purpose, though. Hacking ListModel seems like a temporary solution at best.

                G Offline
                G Offline
                gabor53
                wrote on last edited by gabor53
                #10

                @sierdzio
                I made the following changes:

                function dbGetMeds() {
                    console.log("Entered dbGetMeds.")
                    var db = dbGetHandle()
                    db.transaction(function (tx) {
                        var results = tx.executeSql(
                                    "SELECT MednameField FROM MedNamesTable order by MednameField asc")
                        for (var i = 0; i < results.rows.length; i++) {
                            let obj = {
                                "MednameField": results.rows.item(i).MednameField,
                                "checked": ""
                            }
                        }
                    })
                }
                
                model: ListModel{
                                id: medModel
                            }
                Text {
                                    id: meds
                                    text: medModel.append(obj)
                                    font.pixelSize: 18
                                    color: Setup.delegateText()
                                    anchors.verticalCenter: parent.verticalCenter
                                }
                

                I get no error message at all but there is nothing in the medCombo dropdown either. Am I still missing something?
                Thank you.

                sierdzioS 1 Reply Last reply
                0
                • G gabor53

                  @sierdzio
                  I made the following changes:

                  function dbGetMeds() {
                      console.log("Entered dbGetMeds.")
                      var db = dbGetHandle()
                      db.transaction(function (tx) {
                          var results = tx.executeSql(
                                      "SELECT MednameField FROM MedNamesTable order by MednameField asc")
                          for (var i = 0; i < results.rows.length; i++) {
                              let obj = {
                                  "MednameField": results.rows.item(i).MednameField,
                                  "checked": ""
                              }
                          }
                      })
                  }
                  
                  model: ListModel{
                                  id: medModel
                              }
                  Text {
                                      id: meds
                                      text: medModel.append(obj)
                                      font.pixelSize: 18
                                      color: Setup.delegateText()
                                      anchors.verticalCenter: parent.verticalCenter
                                  }
                  

                  I get no error message at all but there is nothing in the medCombo dropdown either. Am I still missing something?
                  Thank you.

                  sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #11

                  @gabor53 said in Using 2 different ListModels with 2 different ComboBoxes:

                  Am I still missing something?

                  Yes, you are not modifying the model at all now (it is empty). You need to keep those append() calls in your loop!

                   for (var i = 0; i < results.rows.length; i++) {
                              let obj = {
                                  "MednameField": results.rows.item(i).MednameField,
                                  "checked": ""
                              }
                              medModel.append(obj) // HERE!
                          }
                  

                  (Z(:^

                  G 1 Reply Last reply
                  1
                  • sierdzioS sierdzio

                    @gabor53 said in Using 2 different ListModels with 2 different ComboBoxes:

                    Am I still missing something?

                    Yes, you are not modifying the model at all now (it is empty). You need to keep those append() calls in your loop!

                     for (var i = 0; i < results.rows.length; i++) {
                                let obj = {
                                    "MednameField": results.rows.item(i).MednameField,
                                    "checked": ""
                                }
                                medModel.append(obj) // HERE!
                            }
                    
                    G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #12

                    @sierdzio
                    I made the new modifications like this:

                    //Read data from MedNamesTable
                    function dbGetMeds() {
                        console.log("Entered dbGetMeds.")
                        var db = dbGetHandle()
                        db.transaction(function (tx) {
                            var results = tx.executeSql(
                                        "SELECT MednameField FROM MedNamesTable order by MednameField asc")
                            for (var i = 0; i < results.rows.length; i++) {
                                let obj = {
                                    "MednameField": results.rows.item(i).MednameField,
                                    "checked": ""
                                }
                                medModel.append(obj)
                            }
                        })
                    }
                    
                    delegate: QQuick2.ItemDelegate {
                                    width: medCombo.width
                                    height: medCombo.height
                    
                                    //Defining the items in the dropdown list
                                    Text {
                                        id: meds
                                        text: medModel.MednameField
                                        font.pixelSize: 18
                                        color: Setup.delegateText()
                                        anchors.verticalCenter: parent.verticalCenter
                                    }
                                    onClicked: medCombo.currentIndex = index
                                    highlighted: medCombo.highlightedIndex === index
                                }
                    
                    

                    Now it pulls the first item from the MedNamesTable and nothing else. Is

                     text: medModel.MednameField
                    

                    incorrect or something else? Thank you for your help.

                    1 Reply Last reply
                    0
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #13

                      Try calling it with lowercase first letter mednameField. QML engine might scoff at a property beginning with capital letter.

                      Code looks OK so if it displays just one entry - maybe there is only one row in your SQL table?

                      (Z(:^

                      G 1 Reply Last reply
                      0
                      • sierdzioS sierdzio

                        Try calling it with lowercase first letter mednameField. QML engine might scoff at a property beginning with capital letter.

                        Code looks OK so if it displays just one entry - maybe there is only one row in your SQL table?

                        G Offline
                        G Offline
                        gabor53
                        wrote on last edited by
                        #14

                        @sierdzio
                        Same problem. Regardless whether it is MednameField or mednameField it only pulls the last item from the db. I applied it to the patient combobox too with the same results. Even if I delete the

                        text: medModel.MednameField
                        

                        I get the same results. I am really puzzled now...

                        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