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 bindings and scope
Forum Updated to NodeBB v4.3 + New Features

QML bindings and scope

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 1.1k 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.
  • R Offline
    R Offline
    rosetter
    wrote on last edited by
    #1

    Hi all,

    So I'm thoroughly confused about how scope works in qml. Here's a little test:

    @
    import QtQuick 2.0

    Rectangle {
    width: 640/2
    height: 1136/2

    Item {
    width: parent.width
    height: parent.height

    Image {
    id: viewDummy
    anchors.fill: parent
    source: "viewDummy.png"

    ListModel {
    id: comments
    ListElement {
    modelData: "comment 1a"
    }
    ListElement {
    modelData: "comment 1b"
    }
    }
    }

    ListView {
    anchors.fill: parent

    model: viewDummy.comments

    delegate: Rectangle {
    height: 25
    width: 100
    Text { text: modelData }
    }
    }
    }
    }
    @

    The above does not display the contents of the model "comment 1a" and "comment 1b". However, if I change "viewDummy.comments" to just "comments" they are displayed..

    I would expect the reverse to be true... why can the ListView "see" the ListModel that's wrapped in the Image "viewDummy"? Does the nesting not affect scope?

    Thanks for any advice!

    Tyler

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Within a single QML file, ids are just that: IDs. You don't need to specify the "parent" ID, they are sort of global (but, again: only within this QML file).

      By specifying parent.something, you can refer to properties of that parent, like parent.width.

      (Z(:^

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rosetter
        wrote on last edited by
        #3

        IDs are global within a file?! That's a good one to have straight... thanks!

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chrisadams
          wrote on last edited by
          #4

          Hi,

          Actually, ids are unique within a component scope. In most cases, this means "within a file" (since a QML document is a QML type definition which can be instantiated via a component, and thus a given instantiation of the type has its own component scope), but there are some subtleties.

          See http://doc-snapshot.qt-project.org/qt5-stable/qtqml-syntax-objectattributes.html#the-id-attribute and http://doc-snapshot.qt-project.org/qt5-stable/qtqml-documents-scope.html for more information. Delegates are interesting, because they implicitly define a component scope.

          Cheers,
          Chris.

          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