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. Listview delegate using Loader gives null element
Forum Updated to NodeBB v4.3 + New Features

Listview delegate using Loader gives null element

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 2.5k 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.
  • M Offline
    M Offline
    MaxL
    wrote on 1 Apr 2014, 14:09 last edited by
    #1

    Hi everyone,

    I am using a ListView that is supposed to display different type of data so it needs different delegates according to data's type.
    What I did is that the delegate is a Loader that will load a component based on the type of the data. This works well but every time I remove an item from the model ( a custom QAbstractListModel ) I get a Cannot read property 'X' of null. This does not happen if I don't use a Loader but the same delegate for every type of data.

    Does anyone have a suggestion for me ?
    I do follow the beginInsertRows...endInsertRows scheme in the model.

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on 1 Apr 2014, 14:20 last edited by
      #2

      hey, do you access the ListView from the delegate? I had some problems when accessing the ListView via the "parent" property, because when you remove an item from the ListView the delegate item might be detached and the "parent" will be "null" until the item is destroyed.. so it might trigger a parent changed signal and you get an "cannot read property 'X' of null" error.. if that makes any sense :)

      solution in my case, just reference the ListView via the id, and not "parent".

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MaxL
        wrote on 1 Apr 2014, 14:32 last edited by
        #3

        Nop that is not it, nothing references to the ListView as parent instead of the id.

        Here how it is:

        @
        ListView
        {
        id: myListView
        model: myCustomModel
        delegate: itemDelegate
        }

        Component
        {
        id: itemDelegate
        Loader
        {
        id: delegateLoader

            property variant myModel: model
            property variant cellIndex: index
        
            height: 100
            width: myListView.width
            sourceComponent:
            {
                switch( model.type )
                {
                case 0:
                     firstDelegate
                     break
                case 1:
                      secondDelegate
                       break
                }
            }
        }
        

        }

        Component
        {
        id: firstDelegate
        Text
        {
        text: myModel.text
        }
        }

        Component
        {
        id: secondDelegate
        Image
        {
        source: myModel.image
        }
        }
        @

        When removing items, it would say in that example cannot read property 'image' of null or cannot read property 'text' of null

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on 1 Apr 2014, 14:49 last edited by
          #4

          Ok then, I have another idea. Have you tried setting the delegate directly, and not the loader as the delegate? e.g.
          @
          Loader {
          ...
          onLoaded: myListView.delegate = item // loaded item
          }
          @
          I haven't tested this but it should be possible to set the delegate like this?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MaxL
            wrote on 1 Apr 2014, 15:00 last edited by
            #5

            This would not work because Loader is called whenever the ListView encounters a row in its model because that Loader is set as the delegate. So calling myListView.delegate = item does nothing but change the delegate and so invalidate the whole thing ;)

            It has something to do with values binded because of the Loader because it does not happen if I set firstDelegate as myListView's delegate.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MartynW
              wrote on 2 Apr 2014, 10:23 last edited by
              #6

              I'm a bit of a newbie to QML so forgive me if I'm off-beam, but couldn't you simply say:

              @source: myModel == null ? "" : myModel.image@

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MaxL
                wrote on 2 Apr 2014, 12:35 last edited by
                #7

                It does work ( I at least get rid of all the warnings ), but I don't find that solution really clean since if myModel is null then nothing should be loaded ..

                1 Reply Last reply
                0

                1/7

                1 Apr 2014, 14:09

                • Login

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