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. Getting CurrentIndex
QtWS25 Last Chance

Getting CurrentIndex

Scheduled Pinned Locked Moved Solved QML and Qt Quick
listviewcurrentindex
4 Posts 2 Posters 1.2k Views
  • 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 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.
    1 Reply Last reply
    0
    • AndySA Offline
      AndySA Offline
      AndyS
      Moderators
      wrote on last edited by
      #2

      Hi @gabor53 I believe you have you to refer to the model directly rather than via the property, so if you use listModel.append instead then does that help?

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

      G 1 Reply Last reply
      0
      • AndySA AndyS

        Hi @gabor53 I believe you have you to refer to the model directly rather than via the property, so if you use listModel.append instead then does that help?

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

        Hi @AndyS,

        Unfortunately your recommendation didn't work either. I deleted the whole section and I came up with the following:
        Database.js

        function 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)
                    }
                }
        

        ``

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

          @gabor53 If you can provide a complete example then I can see the issue first hand then as it seems like it should work, but it is hard to say without seeing it.

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

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved