Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 2 Support (Qt 5) › Using 2 different ListModels with 2 different ComboBoxes

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #23563

    Gabor

    Hi,

    In the same page I have two separate ComboBoxes. Each ComboBox is populated from a different table from an SQLite db. The first ComboBox looks like this:

            QQuick2.ComboBox {
                id: medicationCombo
                width: backgrnd.width / 2.7
                height: dp(25)
                anchors.bottom: medName.bottom
                anchors.left: medName.right
                anchors.leftMargin: dp(15)
                font.pixelSize: 16
                editable: true
                textRole: "MednameField"
                focus: true
    
                //Displaying the first item from the db.
                currentIndex: 0
    
                model: ListModel {
                    id: listModel2
                }
    
                delegate: QQuick2.ItemDelegate {
                    width: medicationCombo.width
                    height: medicationCombo.height
    
                    //Defining the items in the dropdown list
                    Text {
                        id: medTxt
                        text: model.MednameField //This is not working.
                        font.pixelSize: 18
                        color: "#00008b"
                        anchors.verticalCenter: parent.verticalCenter
                    }
    
                    onClicked: medicationCombo.currentIndex = index
                    highlighted: medicationCombo.highlightedIndex === index
                }

    The second one:

    QQuick2.ComboBox {
                id: patientCombo
                width: backgrnd.width / 2.7
                height: dp(25)
                anchors.bottom: name.bottom
                anchors.left: name.right
                anchors.leftMargin: dp(15)
                font.pixelSize: 16
                editable: true
                textRole: "patientField"
                focus: true
    
                //Displaying the first item from the db.
                currentIndex: 0
    
                model: ListModel {
                    id: listModel
                }
    
                delegate: QQuick2.ItemDelegate {
                    width: patientCombo.width
                    height: patientCombo.height
    
                    //Defining the items in the dropdown list
                    Text {
                        id: ptnts
                        text: model.patientField
                        font.pixelSize: 18
                        color: "#00008b"
                        anchors.verticalCenter: parent.verticalCenter
                    }
    

     

    
    
    When I use only 1 ComboBox on the page it works OK. When I have 2 I keep getting the following error message: 
    Unable to assign [undefined] to QString on line 
     text: model.MednameField //This is not working.
    How can I fix this? Thank you for your help.
    #23564

    jrrobles79

    are you sure thats the field name of the field on that model?  can you post the model?

    #23566

    Gabor

    Hi,

    Here is the other file, JSCode.js:

    //Creating or opening the database
    function dbInit() {
        var db = LocalStorage.openDatabaseSync("Medications", "",
                                               "Storing Medications", 1000000)
        try {
            db.transaction(function (tx) {
                tx.executeSql(
                            'CREATE TABLE IF NOT EXISTS TablePatients (patientField TEXT)')
            })
        } catch (err6) {
            console.log("Error creating TablePatients in database: " + err6)
        }
        ;
    
        try {
            db.transaction(function (tx) {
                tx.executeSql(
                            'CREATE TABLE IF NOT EXISTS MedNamesTable (MednameField TEXT)')
            })
        } catch (err7) {
            console.log("Error creating MedNamesTable in database: " + err7)
        }
        ;
    }
    
    //Opening db
    function dbGetHandle() {
        dbInit()
        try {
            var db = LocalStorage.openDatabaseSync("Medications", "",
                                                   "Storing Medications", 1000000)
        } catch (err4) {
            console.log("Error opening database: " + err4)
        }
        return db
    }
    
    //Insert data into the TablePatients in the db
    function dbInsertPatients(newPatient) {
        dbGetHandle()
        console.log("newItem in Code.js: ", newPatient)
        console.log("TablePatients currentText in Code.js: ", newPatient)
        var db = dbGetHandle()
    
        db.transaction(function (tx) {
            tx.executeSql('INSERT OR IGNORE INTO TablePatients VALUES (?)',
                          [newPatient])
        })
    }
    
    //Read TablePatients from db
    function dbGetPatients() {
        var db = dbGetHandle()
        db.transaction(function (tx) {
            var results = tx.executeSql(
                        "SELECT patientField FROM TablePatients order by patientField asc")
            for (var i = 0; i < results.rows.length; i++) {
                listModel.append({
                                     "patientField": results.rows.item(
                                                         i).patientField,
                                     "checked": ""
                                 })
            }
        })
    }
    
    //Insert data into the MedNamesTable in the db
    function dbInsertMeds(newMed) {
        dbGetHandle()
        console.log("newMed in Code.js: ", newMed)
        console.log("MedNamesTable currentText in Code.js: ", newMed)
        var db = dbGetHandle()
    
        db.transaction(function (tx) {
            tx.executeSql('INSERT OR IGNORE INTO MedNamesTable VALUES (?)',
                          [newMed])
        })
    }
    

    Thank you for your help.

    #23568

    Gabor

    I think the problem is that in the model line

    model: ListModel {
                    id: listModel
                }

    I can’t use

    text: listModel.patientField

    in the delegate only

    text: model.patientField

    works, but I can’t use model for all of the ComboBoxes.

    #23579

    jrrobles79

    I belive that is the way you are populating the ‘listModel2’, I replicate the code of the combobox, but I input values on the listmodels as ListElements, and both combos worked fine, the issue that I see on your original post is that listmodel2 can’t find the field MednameField

     

           model: ListModel {
                                id: listModel2
                                ListElement{
                                    MednameField: "luke"
                                }
                                ListElement{
                                    MednameField: "leia"
                                }
    
                            }
    
                            delegate: QQC2.ItemDelegate {
                                width: medicationCombo.width
                                height: medicationCombo.height
    
                                //Defining the items in the dropdown list
                                Text {
                                    id: medTxt
                                    text: model.MednameField //This is not working.
                                    font.pixelSize: sp(18)
                                    color: "#00008b"
                                    anchors.verticalCenter: parent.verticalCenter
                                }
    
                                onClicked: medicationCombo.currentIndex = index
                                highlighted: medicationCombo.highlightedIndex === index
                            }

     

              model: ListModel {
                                id: listModel
                                ListElement{
                                    patientField: "vader"
                                }
                                ListElement{
                                    patientField: "maul"
                                }
                            }
    
                            delegate: QQC2.ItemDelegate {
                                width: patientCombo.width
                                height: patientCombo.height
    
                                //Defining the items in the dropdown list
                                Text {
                                    id: ptnts
                                    text: model.patientField
                                    font.pixelSize: sp(18)
                                    color: "#00008b"
                                    anchors.verticalCenter: parent.verticalCenter
                                }
                            }

     

    on your sql code Im not sure if is ok since I haven’t work with the storage component of felgo, but looks ok to me, I don’t know since Im not that good as programmer, but maybe the field MednameField should be renamed to mednameField , maybe the first letter uppercase is messing the name of the field, on your other table you have ‘patientName’  and that is working

    and check your function ‘dbGetMedNames()’  I don’t see it on the code your shared, maybe the problem is there

    #23580

    Gabor

    Unfortunately changing the name didn’t help.

    #23581

    jrrobles79

    have you inserted values on that table?, maybe is returning an empty resultset, and thats why it can’t find the field

    #23582

    Gabor

    There is data in the db.

Viewing 8 posts - 1 through 8 (of 8 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded