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. Set context property from QML
Forum Updated to NodeBB v4.3 + New Features

Set context property from QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 3.0k 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
    MoaMoaK
    wrote on 10 Nov 2017, 10:24 last edited by
    #1

    Hi, I would like to set some context properties for an object when I create it. Like for ListView or Gridview, I would like to have a model property available to the delegates.
    Something like :

    // MyObject.qml
    Row {
        property var items
        Repeater {
            model : items
            Delegate {
                // This line is wrong and only here to get the idea
                setContextProperty( "model", items.get(index) )
            }
        }
    }
    
    // Delegate.qml
    Rectangle {
        width: model.width
        height: model.height
    }
    

    I know I can define a property model in the Delegate but I would prefer not to do so and stick with the way basic views works (with the variable model as a context property).
    Can some help me on how to declare such variables from QML ?
    Thx, MoaMoaK

    R 1 Reply Last reply 10 Nov 2017, 11:04
    0
    • M MoaMoaK
      10 Nov 2017, 10:24

      Hi, I would like to set some context properties for an object when I create it. Like for ListView or Gridview, I would like to have a model property available to the delegates.
      Something like :

      // MyObject.qml
      Row {
          property var items
          Repeater {
              model : items
              Delegate {
                  // This line is wrong and only here to get the idea
                  setContextProperty( "model", items.get(index) )
              }
          }
      }
      
      // Delegate.qml
      Rectangle {
          width: model.width
          height: model.height
      }
      

      I know I can define a property model in the Delegate but I would prefer not to do so and stick with the way basic views works (with the variable model as a context property).
      Can some help me on how to declare such variables from QML ?
      Thx, MoaMoaK

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 10 Nov 2017, 11:04 last edited by raven-worx 11 Oct 2017, 13:11
      #2

      @MoaMoaK said in Set context property from QML:

      I know I can define a property model in the Delegate but I would prefer not to do so and stick with the way basic views works (with the variable model as a context property).
      Can some help me on how to declare such variables from QML ?

      Even though in my opinion this is the dirty way to pollute the context property space...

      You need to go around C++ to alter the qml context. So you could add a generic context property (QObject) which offers a method to add new context properties.
      But anyway you need to change your coding, since the context properties are then added later then before. This needs to be considered in your QML design!

      Edit:
      a method which might help you is

      #include <QtQml>
      
      QQmlContext * context = qmlContext(anyObjectComingFromQml);
      context->setContextProperty(name, value);
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MoaMoaK
        wrote on 16 Nov 2017, 16:49 last edited by
        #3

        Sorry, I'm a bit late for the answer, but I found a way to do it fully in QML using Loaders (might works with other kind of items, I haven't tried) and so I can close this topic :

        // MyObject.qml
        Row {
            property var items
            property Component delegate
            Repeater {
                model : items
                Loader {
                    sourceComponent : delegate
                    // The line to add variables
                    property var model : items.get(index)
                }
            }
        }
        
        // Delegate.qml
        Rectangle {
            // model does exists here
            width: model.width
            height: model.height
        }
        
        1 Reply Last reply
        0

        3/3

        16 Nov 2017, 16:49

        • Login

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