Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

SQLite db doesn't populate QML ComboBox



  • Hi,
    I am trying to populate a ComboBox using an SQLite db. The code I currently does't give any error messages. When I click on the combobox I get a white rectangle with nothing in it. Here is the code:
    main.qml:

    import QtQuick 2.0
    import VPlayApps 1.0
    import QtQuick.Controls 1.4
    import QtQuick.Controls 2.2
    import QtQuick.Controls.Styles 1.4
    import QtQuick.Layouts 1.3
    import QtQuick.LocalStorage 2.0
    import QtQml.Models 2.3
    import "Database.js" as JS
    import "Dropboxes.js" as DB
    
    App {
        Rectangle {
            id: root
            color: "#a1d9ea"
            anchors.fill: parent
            focus: false
    
            Text {
                id: title
                text: "Combobox"
                font.pixelSize: 25
                anchors.horizontalCenter: root.horizontalCenter
                anchors.top: root.top
                anchors.topMargin: 30
            }
    
            ComboBox {
                id: whatCombo
                anchors.horizontalCenter: title.horizontalCenter
                anchors.top: title.bottom
                anchors.topMargin: 30
                editable: true
                textRole: "text"
                height: 50
                width: 230
                model: ListModel {
                    id: listModel
                }
    
                delegate: ItemDelegate {
                    width: whatCombo.width
                    height: whatCombo.height
                    text: model.text
                    font.pixelSize: 18
                    font.weight: whatCombo.currentIndex === model.index ? Font.DemiBold : Font
                }
            }
    
            Component.onCompleted: {
                var db = JS.dbGetHandle()
                db.transaction(function (tx) {
                    var results = tx.executeSql(
                                'SELECT what FROM dropboxWhatIs order by what desc')
                    for (var i = 0; i < results.rows.length; i++) {
                        listModel.append({
                                             id: results.rows.item(i).what,
                                             checked: ""
                                         })
                    }
                })
            }
        }
    }
    
    
    

    Database.js:

    function dbInit() {
        var db = LocalStorage.openDatabaseSync("FolkFriends", "",
                                               "Friend info", 1000000)
        try {
            db.transaction(function (tx) {
                tx.executeSql(
                            'CREATE TABLE IF NOT EXISTS dropboxWhatIs (what text)')
            })
        } catch (err) {
            console.log("Error creating table in database: " + err)
        }
        ;
    }
    
    function dbGetHandle() {
        try {
            var db = LocalStorage.openDatabaseSync("FolkFriends", "",
                                                   "Friend info", 1000000)
        } catch (err) {
            console.log("Error opening database: " + err)
        }
        return db
    }
    
    function dbReadAll() {
        var db = dbGetHandle()
        db.transaction(function (tx) {
            var results = tx.executeSql(
                        'SELECT what FROM dropboxWhatIs order by what desc')
            for (var i = 0; i < results.row.length; i++) {
                listModel.append({
                                     what: results.rows.item(i).what,
                                     checked: " "
                                 })
            }
        })
    }
    
    function dbInsert(Pwhat) {
        var db = dbGetHandle()
        var rowid = 0
        db.transaction(function (tx) {
            tx.executeSql('INSERT INTO dropboxWhatIs VALUES(?)', [Pwhat])
            var results = tx.executeSql('SELECT last_insert_rowid()')
            rowid = result.insertId
        })
    }
    
    function dbInsert(Pwhat) {
        var db = dbGetHandle()
        var rowid = 0
        db.transaction(function (tx) {
            tx.executeSql('INSERT INTO dropboxWhatIs VALUES (?)'[Pwhat])
            var results = tx.executeSql('SELECT last_insert_rowid()')
            rowid = result.inserted.Id
        })
        return rowid
    }
    
    

    What am I missing? Thank you for your help.


  • Moderators

    Hi @gabor53,

    Have you checked to see if it returns anything for the select query? To rule out the problem being that the query returns nothing as opposed to it not getting into the combobox?



  • @gabor53 said in SQLite db doesn't populate QML ComboBox:

    You do:

    delegate: ItemDelegate {
             text: model.text
    }
    

    but you define your model as:

    listModel.append({
         id: results.rows.item(i).what,
         checked: " "
     })
    

    and a bit further as

    listModel.append({
        what: results.rows.item(i).what,
        checked: " "
    })
    

    So, not only do you try to use different field names in your model when you add the elements (id vs what) which is not allowed, but also your delegate uses the non-defined model property 'text'.



  • Hi @Diracsbracket ,
    Thank you. It works now.


Log in to reply