SQLite db doesn't populate QML ComboBox
-
wrote on 10 Sept 2018, 20:35 last edited by
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.
-
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?
-
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.
wrote on 11 Sept 2018, 02:58 last edited by Diracsbracket 9 Nov 2018, 03:00@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
vswhat
) which is not allowed, but also your delegate uses the non-defined model property 'text'. -
@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
vswhat
) which is not allowed, but also your delegate uses the non-defined model property 'text'.wrote on 11 Sept 2018, 18:55 last edited byHi @Diracsbracket ,
Thank you. It works now. -
wrote on 5 Nov 2021, 09:27 last edited by
Would you please tell how do you solve the problem?