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. Populate Listview from Database
Forum Updated to NodeBB v4.3 + New Features

Populate Listview from Database

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
listviewdatabaseqmlqt 5.7qt quick
4 Posts 3 Posters 2.7k 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.
  • Q Offline
    Q Offline
    Qjay
    wrote on 30 Jul 2016, 12:37 last edited by Qjay
    #1

    I want to populate listview ( a specific page in my app ) from a database .

    Currently the page is static and look like this

    image: https://s32.postimg.org/vrsdrl2p1/man.png

    code

    import QtQuick 2.6
    import QtQuick.Layouts 1.1
    import QtQuick.Controls 2.0
    
    
    Pane {
        padding: 0
    
        property var delegateComponentMap: {
            "page": itemDelegateComponent
    
        }
    
        Component {
            id: itemDelegateComponent
    
            ItemDelegate {
                text: labelText
                width: parent.width
            }
        }
    
    
    
    
    
    
        ColumnLayout {
            id: column
            spacing: 40
            anchors.fill: parent
            anchors.topMargin: 20
    
            Label {
                id: label1
                Layout.fillWidth: true
                wrapMode: Label.Wrap
                horizontalAlignment: Qt.AlignHCenter
                text: "Offline Pages "
            }
    
            ListView {
                id: listView
    
                Layout.fillWidth: true
                Layout.fillHeight: true
                clip: true
                model: ListModel {
                    ListElement { type: "ItemDelegate"; labelText: "page1" }
                    ListElement { type: "ItemDelegate"; labelText: "page2" }
                    ListElement { type: "ItemDelegate"; labelText: "page3" }
    
    
                }
                spacing: 5
    
                section.property: "type"
    
                delegate: Component{
    
                    Item{
                        id: aItem
                        width: listView.width //rowLayout.width. We got width from children elements before, now get width from listView
                        height: 30
    
                        RowLayout{
    
                            id: rowLayout
                            anchors.fill: parent
                            spacing: 10
    
                            Label{
                                id:page_name
                                padding: 10
                                text: labelText
                                Layout.fillWidth: true // !!! to fill most part of row width
    
                            }
    
    
                                Button{
    
                                    text: qsTr("Delete")
    
                                    id: delete_button
    
                                }
                                Button{
    
                                    text: qsTr("Update")
                                    id: update_button
                                }
    
    
    
                        }
                    }
                }
            }
    
    
    
    
    
    
    
    
    
            RowLayout{
                Layout.alignment: Qt.AlignHCenter | Qt.AlignBottom
    
                Button{
                    text:"Update All"
    
    
                }
                Button{
                    text:"Delete All"
                }
            }
        }
    }
    

    my databse only has 2 entries ( looks like this )

    db : https://s32.postimg.org/jb4nhmoxh/image.png

    how should i approach this task ?

    V 1 Reply Last reply 3 Aug 2016, 19:01
    0
    • _ Offline
      _ Offline
      _antipattern_
      wrote on 3 Aug 2016, 17:30 last edited by
      #2

      I think you might be looking for QSqlTableModel, I'm not sure though how to use it with a ListView.

      1 Reply Last reply
      0
      • Q Qjay
        30 Jul 2016, 12:37

        I want to populate listview ( a specific page in my app ) from a database .

        Currently the page is static and look like this

        image: https://s32.postimg.org/vrsdrl2p1/man.png

        code

        import QtQuick 2.6
        import QtQuick.Layouts 1.1
        import QtQuick.Controls 2.0
        
        
        Pane {
            padding: 0
        
            property var delegateComponentMap: {
                "page": itemDelegateComponent
        
            }
        
            Component {
                id: itemDelegateComponent
        
                ItemDelegate {
                    text: labelText
                    width: parent.width
                }
            }
        
        
        
        
        
        
            ColumnLayout {
                id: column
                spacing: 40
                anchors.fill: parent
                anchors.topMargin: 20
        
                Label {
                    id: label1
                    Layout.fillWidth: true
                    wrapMode: Label.Wrap
                    horizontalAlignment: Qt.AlignHCenter
                    text: "Offline Pages "
                }
        
                ListView {
                    id: listView
        
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    clip: true
                    model: ListModel {
                        ListElement { type: "ItemDelegate"; labelText: "page1" }
                        ListElement { type: "ItemDelegate"; labelText: "page2" }
                        ListElement { type: "ItemDelegate"; labelText: "page3" }
        
        
                    }
                    spacing: 5
        
                    section.property: "type"
        
                    delegate: Component{
        
                        Item{
                            id: aItem
                            width: listView.width //rowLayout.width. We got width from children elements before, now get width from listView
                            height: 30
        
                            RowLayout{
        
                                id: rowLayout
                                anchors.fill: parent
                                spacing: 10
        
                                Label{
                                    id:page_name
                                    padding: 10
                                    text: labelText
                                    Layout.fillWidth: true // !!! to fill most part of row width
        
                                }
        
        
                                    Button{
        
                                        text: qsTr("Delete")
        
                                        id: delete_button
        
                                    }
                                    Button{
        
                                        text: qsTr("Update")
                                        id: update_button
                                    }
        
        
        
                            }
                        }
                    }
                }
        
        
        
        
        
        
        
        
        
                RowLayout{
                    Layout.alignment: Qt.AlignHCenter | Qt.AlignBottom
        
                    Button{
                        text:"Update All"
        
        
                    }
                    Button{
                        text:"Delete All"
                    }
                }
            }
        }
        

        my databse only has 2 entries ( looks like this )

        db : https://s32.postimg.org/jb4nhmoxh/image.png

        how should i approach this task ?

        V Offline
        V Offline
        veryqtperson
        wrote on 3 Aug 2016, 19:01 last edited by
        #3

        Well, you need to connect to DB and execute SQL query in C++ code and pass the data to QML where you can display it. It's not like you can query DB right from QML.
        This might help you.

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          Qjay
          wrote on 4 Aug 2016, 04:26 last edited by
          #4

          thanks @veryqtperson . I will try to implement it

          1 Reply Last reply
          0

          1/4

          30 Jul 2016, 12:37

          • Login

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