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. Problem Accessing Views and Models from Delegates
Forum Updated to NodeBB v4.3 + New Features

Problem Accessing Views and Models from Delegates

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 5.8k 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.
  • A Offline
    A Offline
    ariacorrente
    wrote on 22 Mar 2012, 10:20 last edited by
    #1

    Hi all,
    i have a problem when in a delegate i try to access data from the model or the view. I read the documentation at:

    http://qt-project.org/doc/qt-4.8/qdeclarativemodels.html#accessing-views-and-models-from-delegates

    I copied the following code in a new QML project and run in QtCreatore 2.4.1 with Qt 4.8:
    @import QtQuick 1.1

    Rectangle {
    width: 200; height: 200

    ListModel {
        id: fruitModel
        property string language: "en"
        ListElement {
            name: "Apple"
            cost: 2.45
        }
        ListElement {
            name: "Orange"
            cost: 3.25
        }
        ListElement {
            name: "Banana"
            cost: 1.95
        }
    }
    
    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
    }
    

    }@

    The qml viewer displays the list but i get the following error:

    bq. file:///[...].qml:28: TypeError: Result of expression 'ListView.view' [null] is not an object.
    file:///[...].qml:26: TypeError: Result of expression 'ListView.view' [null] is not an object.

    So, what i'm doing wrong?

    Thanks for you time

    1 Reply Last reply
    0
    • L Offline
      L Offline
      ludde
      wrote on 22 Mar 2012, 11:40 last edited by
      #2

      Not sure why, but I think you cannot access ListView directly from within the Text element, you have to give the Row an id, e.g. delegate, and access ListView as delegate.ListView. Or just use parent.ListView.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dajansen
        wrote on 23 Mar 2012, 01:26 last edited by
        #3

        Yes, you can. You just have to let it know where the view is. Try this!
        @
        Component {
        id: fruitDelegate
        Row {
        id: wrapper
        Text {
        text: " Fruit: " + name
        color: wrapper.ListView.view.fruit_color
        }
        // ... etc
        }
        }
        @

        QtQuick Quality Engineer / Lab Monkey
        Nokia Brisbane

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ariacorrente
          wrote on 23 Mar 2012, 08:00 last edited by
          #4

          Thanks to both of you, fast and precise help. Meanwhile i was searching how to signal the documentation problem i found this bug report:

          https://bugreports.qt-project.org/browse/QTBUG-22163

          Note to self: check bug reports also for documentation.

          1 Reply Last reply
          0

          1/4

          22 Mar 2012, 10:20

          • 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