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. Using SimpleSection with ListView and SQLite db

Using SimpleSection with ListView and SQLite db

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 2 Posters 425 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 14 Sept 2020, 21:24 last edited by
    #1

    Hi,
    I have a db with 2 fields: wordField and sectionField. I want to access the db and the db through a ListView. My QML file:

    import Felgo 3.0
    import QtQuick 2.9
    import QtQuick.Controls 2.5
    import QtQuick.Controls.Styles 1.4
    import QtQuick.LocalStorage 2.12
    import "Database.js" as JS
    
    App {
    
        AppListView {
            id: myListView
            anchors.fill: parent
    
            model: ListModel {
                id: listModel
            }
    
            delegate: SimpleRow {
                text: model.wordField
            }
    
            section.property: listModel.sectionField
            section.delegate: SimpleSection {
            }
        }
    
        Component.onCompleted: {
            JS.dbGetWordList()
        }
    }
    
    

    At this point it lists all the words from the db. There are no sections.

    section.property: listModel.sectionField
    

    gives the following error message: Unable to assign [undefined] to QString.
    Clearly it is unable to access the sectionField in the db. I tried to change listModel to model but it doesn't work either. How can I define the section.property in this case correctly?
    I appreciate any help. Thank you.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      GrecKo
      Qt Champions 2018
      wrote on 15 Sept 2020, 08:47 last edited by
      #2

      ListModel has no sectionField property. What is it supposed to be?

      Also how do you pouplate your model? Are you somehow accessing listModel is JS.dbGetWordList()?

      G 1 Reply Last reply 15 Sept 2020, 19:15
      0
      • G GrecKo
        15 Sept 2020, 08:47

        ListModel has no sectionField property. What is it supposed to be?

        Also how do you pouplate your model? Are you somehow accessing listModel is JS.dbGetWordList()?

        G Offline
        G Offline
        gabor53
        wrote on 15 Sept 2020, 19:15 last edited by gabor53
        #3

        Hi @GrecKo ,
        I have JavaScript to access and add data to the db. sectionField is a field in the db. Here is the code:

        function dbInit() {
            var db = LocalStorage.openDatabaseSync("ListViewTest", "",
                                                   "Wordlist", 1000000)
            try {
                db.transaction(function (tx) {
                    tx.executeSql('CREATE TABLE wordTable ( wordField TEXT)')
                    console.log("Table created.")
                })
            } catch (err) {
                console.log("Error creating table in database: " + err)
            }
            ;
        }
        
        //Open database
        function dbGetHandle() {
            dbInit()
            try {
                var db = LocalStorage.openDatabaseSync("ListViewTest", "",
                                                       "Wordlist", 1000000)
            } catch (err) {
                console.log("Error opening database: " + err)
            }
            return db
        }
        
        //Read the wordField content from the db
        function dbGetWordList() {
        
            var db = dbGetHandle()
        
            db.transaction(function (tx) {
                var results = tx.executeSql(
                            'SELECT  wordField, sectionField FROM wordTable ORDER BY sectionField ASC ')
                for (var i = 0; i < results.rows.length; i++) {
        
                    listModel.append({
                                     "sectionField": results.rows.item(i).sectionField,
                                     "wordField": results.rows.item(i).wordField,
                                     "checked": ""
                                 })
                }
            })
        }
        
        function modelSort() {
            var model = []
            for (var i = 0; i < 26; i++) {
                var entry = {
                    "text": String.fromCharCode(65 + i)
                }
                model.push(entry)
            }
            return model
        }
        
        

        Basically what I want to do is using the sectionField db field as the section property for listView.
        Please advise how to do this correctly. Thank you.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GrecKo
          Qt Champions 2018
          wrote on 16 Sept 2020, 07:39 last edited by
          #4

          section.property take the name of a role of the model.

          if sectionField is a role name of your model, do section.property: "sectionField"

          G 1 Reply Last reply 16 Sept 2020, 14:42
          0
          • G GrecKo
            16 Sept 2020, 07:39

            section.property take the name of a role of the model.

            if sectionField is a role name of your model, do section.property: "sectionField"

            G Offline
            G Offline
            gabor53
            wrote on 16 Sept 2020, 14:42 last edited by
            #5

            @GrecKo Can I use the field name from the model table as a role name? Or how can I turn the field name (sectionField) into a role?

            1 Reply Last reply
            0

            1/5

            14 Sept 2020, 21:24

            • Login

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