Getting CurrentIndex
Solved
QML and Qt Quick
-
Hi,
I intend to use the code below to get an item from an editable combobox and if it is not a duplicate update the model and write it into the db.
import QtQuick 2.9 import QtQuick 2.2 import QtQuick 2.0 import VPlayApps 1.0 import QtQuick.Controls.Styles 1.4 import QtQuick.Controls.Styles 1.0 import QtQuick.Dialogs 1.3 import QtQuick.Controls 1.1 import QtQuick.Controls 1.4 import QtQuick.Controls 2.2 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 font.pixelSize: 22 editable: true textRole: "what" height: 50 width: 230 model: ListModel { id: listModel } ListView { id: listView } delegate: ItemDelegate { id: whatDelegate width: whatCombo.width height: whatCombo.height text: model.what font.pixelSize: 22 } onAccepted: { if (find(editText) === -1) { model.append({ what: editText }) var newItem = editText var insertedrow = false if (listView.model.get(listView.currentIndex).what < 1) { if (input.insertrec()) { input.setlistview() insertedrow = true } else { statustext.text = "Failed to insert row." } } else { input.setlistview() JS.dbUpdate(listView.model.get( listView.currentIndex).what) } if (insertedRow) { input.initrec() creatingNewEntry = false editingEntry = false listView.forceLayout() } } } 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({ what: results.rows.item(i).what, checked: "" }) } }) } } } } `` Each time I write something into the combobox and press Enter I get an error message: **Type Error: cannot call method get of undefined.** How can I avoid this error? Thank you for your help.
-
Hi @AndyS,
Unfortunately your recommendation didn't work either. I deleted the whole section and I came up with the following:
Database.jsfunction dbAddNew(newItem) { var db = dbGetHandle() db.transaction(function (tx) { tx.executeSql('INSERT INTO dropboxWhatIs VALUES (?)', [newItem]) }) } `` The new section in main.qml:
onAccepted: { console.log("Entered onAccepted. CurrentIndex: " + currentIndex) if (find(editText) === -1) { model.append({ what: editText }) console.log("editText: " + editText) var newItem = editText JS.dbGetHandle() JS.dbAddNew(newItem) } }
``