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. Reference error
Forum Updated to NodeBB v4.3 + New Features

Reference error

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 3 Posters 3.0k 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.
  • F Offline
    F Offline
    franziss
    wrote on last edited by
    #1

    Dear all,

    I am trying to create a button, which when pressed, will clear my list elements. But, when I pressed it, it has the error

    ReferenceError: Can't find variable: MediaIconModel

    Why is it so? It can access the MediaIconModel under GridView. THank you for your kind help
    @
    Component {
    id: mediaIconDelegate
    Item {
    width: grid.cellWidth; height: grid.cellHeight
    Column {
    anchors.fill: parent
    Image { source: portrait; anchors.horizontalCenter: parent.horizontalCenter;
    width: 64; height: 64
    }
    Text { text: name; anchors.horizontalCenter: parent.horizontalCenter }
    }

               MouseArea {
                   id: iconMouseArea
                   anchors.fill: parent
                   width: 64
                   height: 64
                   hoverEnabled: true
    
                   onEntered: {
                       var iconObj = Qt.createComponent("icon.qml");
                       iconObj.createObject(parent);
                   }
               }
           }
       }
    
       GridView {
           id: grid
           anchors.fill: parent
           cellWidth: 64; cellHeight: 64
    
           model: MediaIconModel {}
           delegate: mediaIconDelegate
       }
    
       Image {
           id: clearAllIcon
           y: 200
           width: 64
           height: 64
           source: "clearButton.gif"
    
           MouseArea {
               id: clearAllIconMouseArea
               anchors.fill: parent
               width: 64
               height: 64
    
               onClicked: {
                   MediaIconModel.clear();
               }
           }
       }
    

    @

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chuck Gao
      wrote on last edited by
      #2

      MediaIconModel is not a member/model's name, it's your custom model's name.

      Chuck

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tdmatsu
        wrote on last edited by
        #3

        you can find a simple example here: http://doc.qt.nokia.com/latest/qdeclarativemodels.html

        are you trying to use a custom model?

        1 Reply Last reply
        0
        • F Offline
          F Offline
          franziss
          wrote on last edited by
          #4

          Yes, MediaIconModel is my custom model's name, which I declared in MediaIconModel.qml

          @
          import QtQuick 1.0

          ListModel {
          ListElement {
          name: "Add"
          portrait: "addButton.jpg"
          }
          }
          @

          I could not access mediaIconModel in my main qml Image element. But if instead of declaring mediaIconModel in its own qml, I declare it in my main qml
          @
          ListModel {
          id: mediaIconModel
          ListElement {
          name: "Jim Williams"
          portrait: "liverbird.gif"
          }
          }
          @

          And in my main qml GridView element I declare model: mediaIconModel, I can access the Image element. Why is it so?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chuck Gao
            wrote on last edited by
            #5

            Try this:
            @
            MediaIconModel {
            id: myModel
            .......
            }

            GridView {
            id: grid
            anchors.fill: parent
            cellWidth: 64; cellHeight: 64

            model: myModel
            delegate: mediaIconDelegate
            

            }
            @

            Chuck

            1 Reply Last reply
            0
            • F Offline
              F Offline
              franziss
              wrote on last edited by
              #6

              Hi Chuck,

              Thank you for your suggestion, but it doesn't work. Let us look at the following demo code. In file main.qml

              @
              import QtQuick 1.0

              Rectangle {
              width: 360
              height: 360
              Text {
              text: "Hello World"
              anchors.centerIn: parent
              }

              MouseArea {
                  anchors.fill: parent
                  onClicked: {
                      fruitModel.clear();
                  }
              }
              
              Rectangle {
                    width: 200; height: 200
                   Component {
                       id: fruitDelegate
                       Row {
                               Text { text: " Fruit: " + name; color: ListView.view.fruit_color }
                               Text { text: " Cost: $" + cost }
                               Text { text: " Language: " + ListView.view.model.language }
                       }
                   }
              
                   ListView {
                       property color fruit_color: "green"
                       model: FruitModel {}
                       delegate: fruitDelegate
                       anchors.fill: parent
                   }
               }
              

              }
              @

              And file FruitModel.qml

              @
              import QtQuick 1.0

              ListModel {
              id: fruitModel
              property string language: "en"
              ListElement {
              name: "Apple"
              cost: 2.45
              }
              }
              @

              How come

              @
              MouseArea {
              anchors.fill: parent
              onClicked: {
              fruitModel.clear();
              }
              }
              @

              Doesn't work in main.qml? Thank you for your help

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tdmatsu
                wrote on last edited by
                #7

                ID needs to be defined in the instance, not in the custom component.
                You could try something like this:

                FruitModel.qml
                @
                import QtQuick 1.0

                ListModel {
                property string language: "en"
                ListElement {
                name: "Apple"
                cost: 2.45
                }
                }
                @

                main.qml
                @
                import QtQuick 1.0

                Rectangle {
                width: 360
                height: 360

                Component {
                id: fruitDelegate
                Row {
                Text { text: " Fruit: " + name; color: ListView.view.fruit_color }
                Text { text: " Cost: $" + cost }
                Text { text: " Language: " + ListView.view.model.language }
                }
                }

                ListView {
                property color fruit_color: "green"
                model: FruitModel {id:fruitModel}
                delegate: fruitDelegate
                anchors.fill: parent
                }

                MouseArea {
                anchors.fill: parent
                onClicked: {
                fruitModel.clear();
                }
                }
                }
                @

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  franziss
                  wrote on last edited by
                  #8

                  tdmatsu, you are a genius! Thank you very much for your kind help. Have a nice day! =)

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tdmatsu
                    wrote on last edited by
                    #9

                    no problem! have a nice day :)

                    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