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, GridView delegate: ReferenceError: `<variable_name>` is not defined

QML, GridView delegate: ReferenceError: `<variable_name>` is not defined

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 660 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.
  • S Offline
    S Offline
    szymonkacperek
    wrote on 23 Dec 2022, 09:04 last edited by szymonkacperek
    #1

    Good day,

    I am struggling with it a bit so far, but couldn't find an answer. Here is the core situation.

    I have a GridView with ListModel:

    GridView {
        id: shapeSidePos
        ...
        model: shapeSidePosList
                    
        delegate: ButtonIcon {
            style: ButtonIcon.Styles.Parallel
            icon: path
            ...
            }
    
        ListModel {
            id: shapeSidePosList
            ListElement { path: "../../images/shape_side_positioning_base.png" }
            ListElement { path: "../../images/shape_side_positioning_right.png" }
            ...
        }
    }
    

    And in a runtime I get error:

    ReferenceError: path is not defined
    

    The thing is, when I have used e.g. Rectangle instead of ButtonIcon, everything worked fine! Accessing elements with model.path did not work either. In ButtonIcon, icon is defined so:

    property alias icon: buttonIcon.source
    

    where buttonIcon is an image with MouseArea.

    Been searching through web, found some tricks with JS but that makes things complicated and many developers may use this code later. It would be problematic for them. However, I can access the right path while referring directly to an index:

    icon: shapeSidePosList.get(0).path
    

    I tried packing delegate into Component, tried DelegateModel, nothing has given the expected result.

    I have no idea what is going on,

    I would be very thankful for any help provided.

    Best regards
    Szymon

    1 Reply Last reply
    0
    • S Offline
      S Offline
      szymonkacperek
      wrote on 23 Dec 2022, 14:03 last edited by
      #2

      Got an answer from Stack Overflow: https://stackoverflow.com/questions/74897846/

      You could try to enforce the path property in the delegate with the required keyword like shown here https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#view-delegates and see if it yields any other errors."
      

      So I have done:

          delegate: ButtonIcon {
              required property var path // <-- this
              required property int index // <-- and this
      
              style: ButtonIcon.Styles.Parallel
              icon: path
              ...
              }
      

      and the problem is solved.

      Best regards
      Szymon

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GrecKo
        Qt Champions 2018
        wrote on 23 Dec 2022, 14:30 last edited by
        #3

        The reason is most likely because ButtonIcon has a required property itself. When a delegate has one it doesn't provide model roles as context properties anymore but only set those corresponding to explicit required properties.

        1 Reply Last reply
        1
        • S Offline
          S Offline
          szymonkacperek
          wrote on 27 Dec 2022, 06:48 last edited by
          #4

          Ok, this is indeed interesting. Thank you for clearance.

          Best regards
          Szymon

          1 Reply Last reply
          0

          1/4

          23 Dec 2022, 09:04

          • 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