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. Setting property based on role
Forum Updated to NodeBB v4.3 + New Features

Setting property based on role

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 1.8k 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.
  • B Offline
    B Offline
    Babalas
    wrote on last edited by
    #1

    I might be missing something but I can't find out to set the property based on the role coming in from TableView. If I was using a ListModel then I would have
    @ListModel { id: mymodel ...}
    ...
    onAccepted: { myModel.setProperty(rowIndex, role, value) }
    @

    Except I'm using a QObjectList so instead I would expect
    @onAccepted: { myModel[rowIndex].setProperty(role, value) }@
    to work but setProperty doesn't exist.

    So how do I set the property dynamically like this?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      portoist
      wrote on last edited by
      #2

      By QObjectList you mean QList<QObject*> exposed to QML? If so, then you need you need to have something like this in class you use in your list:
      @
      Class Ajax: public QObject{
      Q_OBJECT
      Q_PROPERTY(QString property READ property WRITE setProperty NOTIFY propertyChanged)
      public:
      ....
      QString property() const; void setProperty(QString const &value);
      signal:
      propertyChanged
      }
      @
      And than in QML you should be able to do:
      @
      onAccepted: { myModel[rowIndex].property=value }
      @

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Babalas
        wrote on last edited by
        #3

        Its the 'property' part I want to call dynamically. I.e. in my class I've got properties named, "key", "value", "section". The table then displays those values. Role in this case contains one of those values and so I want to set the appropriate property based on that role. If I was using the ListModel I would be able to do this by calling setProperty on the model.
        You did give me an idea though. Maybe I can expose the QObject::setProperty method to QML

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fludwig
          wrote on last edited by
          #4

          Hi Babalas,
          did you find a solution over the last months? I'm sitting on the same problem at the moment...

          I also would like to call modelname.setProperty on a QObject list exposed from C++.

          Best regards
          Florian.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Babalas
            wrote on last edited by
            #5

            Sort of. I got around the problem by creating custom delegates that I then let handle input.
            So the table
            @
            TableView {
            ...
            TableViewColumn {
            role: "value"; title: "Value"; delegate: selectDelegate
            }
            @

            The delegate
            @
            Component {
            id: selectDelegate
            Item {
            function select(model) {
            switch(model.definition.type) .... // this is just an enum defined in my code
            return aComponent
            }

            Component {
            id: aComponent
            // Something that can handle clicking and so forth
            }

            Loader {
              property var localModel
              sourceComponent: select(model[styleData.row])
              Binding on localModel {
                when: status == Loader.Ready
                 value: model[styleData.row]
            }
            

            }
            }
            @

            Since I end up with a delegate of my own I get to choose explicitly how localModel maps to my controls. In my case its a property editor so I have multiple types of components to handle color, text, date, etc so you could simplify this down.

            1 Reply Last reply
            0

            • Login

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