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. SQLite db doesn't populate QML ComboBox
Forum Updated to NodeBB v4.3 + New Features

SQLite db doesn't populate QML ComboBox

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 4 Posters 1.1k 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    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.

    DiracsbracketD 1 Reply Last reply
    0
    • AndySA Offline
      AndySA Offline
      AndyS
      Moderators
      wrote on last edited by
      #2

      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?

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

      1 Reply Last reply
      0
      • G gabor53

        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.

        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on last edited by Diracsbracket
        #3

        @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'.

        G 1 Reply Last reply
        3
        • DiracsbracketD Diracsbracket

          @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'.

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

          Hi @Diracsbracket ,
          Thank you. It works now.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Vahid KaraBey
            wrote on last edited by
            #5

            Would you please tell how do you solve the problem?

            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