Results 1 to 4 of 4

Thread: calling setContextProperty() multiple times with same name

  1. #1
    Join Date
    Sep 2013
    Posts
    9
    Thanked 1 Time in 1 Post

    Default calling setContextProperty() multiple times with same name

    Hi,

    I’m working on an entity-component based application. Basically an entity is a list of components and a component is just an object which has some simple data (ints, strings, floats,…). I have a list which shows all the entities in my application and I have a custom qml component which i call the “component editor”. The component editor is used to edit the simple data from a component of a selected entity(i use two way binding for this). The component editor can scroll through all the components of the selected entity (basically, there is a forward/back button which shows the next/previous component of the selected entity). I hope the basic idea is clear.

    In my framework, I have a factory object, that for each component (each component has a type) generates a qmlInterfaceObject. The qml interface object is a qobject, which has the component as a member, it knows the qml file location for the component editor (the component editor has a loader which loads the qml file of the current component qml interface) and it has a string which identifies the context property which is used in the qml file to communicate with this qml interface (class variable).

    When I select an entity in my list, the factory generates a qmlinterfaceobject (a pointer) for the first component of the entity. I do:

    Qt Code:
    1. ...->rootContext()->setContextProperty(qmlinterfaceobject ->getNameOfContextProperty(), qmlinterfaceobject);
    To copy to clipboard, switch view to plain text mode 

    Now when I select another entity, another qmlinterfaceobject is created. If it is generated for the same type of component as the current qmlInterfaceObject, I call setContextProperty with the same name, but another pointervalue. When this happens, i get some very strange results (basically the new component is updated with the values of the old ones which are still in the interface). Is it not allowed to setcontextproperty twice for a given name? Is it maybe in this case better to create a new context every time i switch entities? Or is there a better solution for this?
    Cheers,

    Matt

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: calling setContextProperty() multiple times with same name

    This sounds more like something a property on a single context object could do.

    Something like

    Qt Code:
    1. class ApplicationContext : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(QObject* currentInterface READ currentInterface NOTIFY currentInterfaceChanged)
    5.  
    6. public slots:
    7. void nextComponent();
    8. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. ApplicationContext context;
    2. ....rootContext()->setContextProperty("app", &context);
    To copy to clipboard, switch view to plain text mode 
    Another option would be to register the interface types with QML can have each editor instantiate its own.
    Then either get the component as a variant into the interface instance or set some form of ID on the interface instance and let it retrieve the data object.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2013
    Posts
    9
    Thanked 1 Time in 1 Post

    Default Re: calling setContextProperty() multiple times with same name

    In my loader, could I do then something like:

    Qt Code:
    1. Loader
    2. {
    3. source: app.currentInterface.getLocationOfQMLFile()
    4. }
    To copy to clipboard, switch view to plain text mode 

    (if getLocationOfQMLFile is a qinvokable of currentInterface)? Otherwise I don't get your first suggestion (is it then not just adding another, unnecessary layer)...
    Thanks for the suggestions though,

    Matt

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: calling setContextProperty() multiple times with same name

    Yes, for example, maybe as a property instead of a function though.

    The context object's property is notifyable, thus usable in property bindings.

    You can of course have the QML to load as an additional property on the context object.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 11th November 2015, 05:51
  2. Running Thread multiple Times
    By totosugito in forum Newbie
    Replies: 1
    Last Post: 6th February 2015, 08:56
  3. How to use a class multiple times?
    By xtlc in forum Newbie
    Replies: 5
    Last Post: 8th August 2013, 15:48
  4. Replies: 3
    Last Post: 26th October 2009, 19:11
  5. Replies: 5
    Last Post: 6th March 2007, 05:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.