Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt Quick using delegate and call 'parent'

Qt Quick using delegate and call 'parent'

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
2 Posts 2 Posters 637 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.
  • T Offline
    T Offline
    taedooly
    wrote on last edited by
    #1
    ListView {
                id: cowListView
                width: parent.width
                Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                model: _ResourceVec.getSize()
                delegate: ItemDelegate {
                    width: parent.width
                    anchors.horizontalCenter: parent.horizontalCenter
    

    When i use delegate in list view and then call parent in it, this error occur.

    TypeError: Cannot read property 'width' of null
    TypeError: Cannot read property 'horizontalCenter' of null
    

    it seems like have problem getting 'parent '.
    But somehow my code works perfectly as i intended
    I'm ignoring that error but i know why it happens and what is the perfect solution.

    i use QT 5.14.2 and used library is

    import QtQuick 2.12
    import QtQuick.Layouts 1.14
    import QtQuick.Controls 2.12
    
    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      Instances of the delegate aren't direct children of the view. The implementation looks something like this:

      ListView
       |-ListView contentItem
           |-delegate instance 0
           |-delegate instance 1
           | ...
           |-delegate count - 2
           |-delegate count - 1
      

      To have a delegate instance refer to the properties of the ListView, either reference it by id:

      delegate: ItemDelegate {
        width: cowListView.width
      }
      

      or use the attached properties:

      delegate: ItemDelegate {
        width: ListView.view.width # ListView.view == cowListView
      }
      

      Using attached properties is advantageous for portability. Renaming cowListView won't break its delegate. On the other hand, the attached properties are only attached to the top level delegate. Descendents of the delegate generally need to explicitly reference the top of the delegate tree in some form.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      1

      • Login

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