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. QML ListView - open file by selected Elemnt
Forum Updated to NodeBB v4.3 + New Features

QML ListView - open file by selected Elemnt

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 365 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.
  • D Offline
    D Offline
    Damian7546
    wrote on last edited by
    #1

    Hi,
    I have example with ListView like below:

    import QtQuick 2.15
    
    Item {
        ListModel {
            id: settingsMainMenuListModel
            ListElement {
                menuText: "Temperature Units"
                iconSource: "qrc:/UI/Assets/thermometer.png"
            }
            ListElement {
                menuText: "Schedule"
                iconSource: "qrc:/UI/Assets/calendar.png"
            }
            ListElement {
                menuText: "WiFi Settings"
                iconSource: "qrc:/UI/Assets/wifi.png"
            }
            ListElement {
                menuText: "About"
                iconSource: "qrc:/UI/Assets/info-button.png"
            }
        }
    
        Rectangle {
            id: stackViewBackgound
            width: parent.width * .7
            height: parent.height * .7
            radius: 5
            anchors.centerIn: parent
            color: "black"
            border.width: 3
            border.color: "white"
        }
    
        ListView {
            id: settingsListView
            anchors.fill: stackViewBackgound
            model: settingsMainMenuListModel
            interactive: false
            delegate: Rectangle {
                id: mainButton
                width: stackViewBackgound.width
                height: stackViewBackgound.height / 4
                color: "black"
                border.color: "white"
                border.width: 4
                radius: 5
                Image {
                    id: iconImage
                    anchors {
                        verticalCenter: parent.verticalCenter
                        left: parent.left
                        leftMargin: 30
                    }
                    source: iconSource
                    height: 48
                    width: 48
                }
                Text {
                    id: mainText
                    anchors {
                        verticalCenter: parent.verticalCenter
                        left: iconImage.right
                        leftMargin: 40
                    }
                    color: "white"
                    font.pixelSize: 30
                    text: menuText
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        settingsStackView.push("AboutPage.qml")
                    }
                }
            }
        }
    }
    
    

    It contains four elements, now when I click any item , always the AbautPage.qml is opened.
    How to make:
    Click on element 1 open .qml file1
    Click on element 2 open .qml file2
    Click on element 3 open .qml file3
    Click on element 4 open .qml AbautPage

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Damian7546
      wrote on last edited by
      #2

      I solved my problem like this:

      import QtQuick 2.15
      
      Item {
          ListModel {
              id: settingsMainMenuListModel
              ListElement {
                  menuText: "Temperature Units"
                  iconSource: "qrc:/UI/Assets/thermometer.png"
                  openPicture: function (value)  {settingsStackView.push("page1.qml")}
              }
              ListElement {
                  menuText: "Schedule"
                  iconSource: "qrc:/UI/Assets/calendar.png"
                  openPicture: function (value)  {settingsStackView.push("page2.qml")}
              }
              ListElement {
                  menuText: "WiFi Settings"
                  iconSource: "qrc:/UI/Assets/wifi.png"
                   openPicture: function (value)  {settingsStackView.push("page3.qml")}
              }
              ListElement {
                  menuText: "About"
                  iconSource: "qrc:/UI/Assets/info-button.png"
                   openPicture: function (value)  {settingsStackView.push("page4.qml")}
              }
          }
      
          Rectangle {
              id: stackViewBackgound
              width: parent.width * .7
              height: parent.height * .7
              radius: 5
              anchors.centerIn: parent
              color: "black"
              border.width: 3
              border.color: "white"
          }
      
          ListView {
              id: settingsListView
              anchors.fill: stackViewBackgound
              model: settingsMainMenuListModel
              interactive: false
              delegate: Rectangle {
                  id: mainButton
                  width: stackViewBackgound.width
                  height: stackViewBackgound.height / 4
                  color: "black"
                  border.color: "white"
                  border.width: 4
                  radius: 5
                  Image {
                      id: iconImage
                      anchors {
                          verticalCenter: parent.verticalCenter
                          left: parent.left
                          leftMargin: 30
                      }
                      source: iconSource
                      height: 48
                      width: 48
                  }
                  Text {
                      id: mainText
                      anchors {
                          verticalCenter: parent.verticalCenter
                          left: iconImage.right
                          leftMargin: 40
                      }
                      color: "white"
                      font.pixelSize: 30
                      text: menuText
                  }
                  MouseArea {
                      anchors.fill: parent
                      onClicked: {
                          openPicture(index)
                      }
                  }
              }
          }
      }
      
      1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #3

        @Damian7546 said in QML ListView - open file by selected Elemnt:

        settingsListView

                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            /* here you need to set */
                            settingsListView.currentIndex = index
                            openPicture(index)
                        }
                    }
        
        1 Reply Last reply
        0

        • Login

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