Setting property based on role



  • 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?



  • 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 }
    @



  • 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



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.