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. Why does `required` change the value of a property?
Qt 6.11 is out! See what's new in the release blog

Why does `required` change the value of a property?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 661 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
    mjs225
    wrote on last edited by mjs225
    #1

    I'm learning this example https://doc.qt.io/qtforpython-6/examples/example_quick_models_objectlistmodel.html#example-quick-models-objectlistmodel.

    I made a small change to the example: the delegate was implemented in a separate QML. Then the code stopped working. After some trials, I found it was because the property was declared as required. Why does required change the value of a property? Thanks very much.

    // view.qml
    import QtQuick
    
    ListView {
        width: 100; height: 100
    
        delegate: MyItem {
            mdl: model
        }
    }
    // MyItem.qml
    import QtQuick
    Rectangle {
        property var mdl // adding `required` will make mdl undefined
        color: mdl.modelData.color
        height: 25
        width: 100
        Text { text: mdl.modelData.name }
    }
    

    The python file is the same as the example.

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #3

      and do fix it you should do:

          delegate: MyItem {
              required property var model
              mdl: model
          }
      

      (or rename mdl to model in MyItem.qml)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kai Nickel
        wrote on last edited by
        #2

        Strange but true: by using "required" in a delegate, you basically opt in a different mode of operation.

        See this section of the documentation for details: https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#models

        Especially note this paragraph from the documentation: "The model, index, and modelData roles are not accessible if the delegate contains required properties, unless it has also required properties with matching names."

        This is what happens in your example: "model" is undefined and so "mdl" becomes undefined.

        1 Reply Last reply
        1
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #3

          and do fix it you should do:

              delegate: MyItem {
                  required property var model
                  mdl: model
              }
          

          (or rename mdl to model in MyItem.qml)

          1 Reply Last reply
          0
          • M mjs225 has marked this topic as solved on

          • Login

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