3

I really confuse right now

What's is the difference between

QQmlApplicationEngine engine;
engine.rootContext().setContextProperty("myObject",&userData);

and

object->setProperty("myObject", myObject)

Here is the QML file

ApplicationWindow {
id: applicationWindow1

Item {
    id: propertyHolder
    property MyObject myObject
}

I had read how to use QML binding but still hasn't figure it out. Please help Thanks

EDIT : ======================= I attached snippet code here

ApplicationWindow {
id: applicationWindow1

Item {
    id: propertyHolder
    property MyClass myClass
}

Button {
    onClicked : 
        propertyHolder.myClass.doSomething()
}

main.cpp

QQmlApplicationEngine engine;
QQmlContext* context = engine.rootContext();

MyClass myClass;
context->setContextProperty("myClass",&myClass);
engine.load(QUrl("qrc:///mainControl.qml"));

And when i clicked on the button, it gave me a null error for calling method Where did i go wrong?

Nejat
  • 31,784
  • 12
  • 106
  • 138
illunara
  • 327
  • 1
  • 5
  • 15
  • `myClass` is registered as a context property in the root context. This mean that you can access **with the defined name**, i.e. `myClass` from whereever you want in your QML code: just remove that `propertyHolder` item and leave `onClicked: myClass.doSomething()`. If you still have `undefined` for the called function, than either the called function is not a `SLOT` or it is not declared as `Q_INVOKABLE`. – BaCaRoZzo Mar 08 '15 at 20:00
  • Even in different QML file? ( I will test it now ) However, myClass also a data holder, i store many data inside it, and change base on user's action ( like login, logout, getData....). That's why i want to keep one instance of it and share between QML's component, or else, i will have to setContextProperty everytimes data get changed – illunara Mar 09 '15 at 02:00
  • A context property is not re-instanced. Once registered it is updated, according to its code, until its context is destroyed. Since the property is set in the root context it is available throughtout the whole cycle of the program and across all the QML code. Contexts are organised in a hierarchy and each context can access variables in parent contexts, i.e. every context can access variables available in root context. It works this way. If it does not work for you, then another problem should be in place. – BaCaRoZzo Mar 09 '15 at 08:53
  • As i just test, context property didn't update when i change C++ object's property. This is just over-head – illunara Mar 09 '15 at 09:17
  • How do you update? If you update via `Q_INVOKABLE` or `SLOT`s it should work. Show the code, both C++ and QML. – BaCaRoZzo Mar 09 '15 at 10:07
  • None, both of them. I think i chose the wrong way to approach this problem. I will update the code to gist, in mean while, can i ask you directly by email or skype? – illunara Mar 09 '15 at 10:36
  • Update the code. There's nothing more I can tell you via mail/skype. – BaCaRoZzo Mar 09 '15 at 12:32
  • Here, i put them to git, please take a look main.cpp https://gist.github.com/illunara/881354bc7ba5b7bada50 model.h https://gist.github.com/illunara/6a1dc2c69ebd56fb1637 userData.h https://gist.github.com/illunara/d5fc447a67f2d4b63fca main.qml https://gist.github.com/illunara/366a486900a58c52c988 Please tell me if you need anything else – illunara Mar 09 '15 at 13:25
  • Appreciated the effort. I'll give a deeper look ASAP. – BaCaRoZzo Mar 09 '15 at 13:51
  • Skimming the available code it is difficult to see the error, both because it does not compile due to lack of sources and because the available code base is not so small for such a simple error. Where does the problem occur? I've seen that u use the context property extensively. Also, without sources it's difficult to know what you did in the updating functions. Try to shrink the provided code to the minimum example, the [SSCCE](http://www.sscce.org). – BaCaRoZzo Mar 09 '15 at 22:02
  • Nothing much, really. I just query from database, parse, and re-assign data. As you can see, i set 2 contextProperty userData and employeeModel from main, but when i call login() method, it will update data inside employeeModel, however in QML nothing get update, old data get debug. – illunara Mar 10 '15 at 03:28

1 Answers1

2

setProperty is a member of QObject and is used to set a value for a property of a QObject. While setContextProperty is a member of QQmlContext class and is used to set the value of a name property on a qml context. You can read in the Qt documentation about QQmlContext :

Each QQmlContext contains a set of properties, distinct from its QObject properties, that allow data to be explicitly bound to a context by name. The context properties are defined and updated by calling QQmlContext::setContextProperty().

Nejat
  • 31,784
  • 12
  • 106
  • 138
  • thank Nejat. in my case, i want to store an c++ object and share between qml componen, which way should i go with? – illunara Mar 08 '15 at 13:50
  • 1
    You should use `setContextProperty`. – Nejat Mar 08 '15 at 14:13
  • I have problem with setConTextProperty, actually i don't know how show use it effective yet. Sometime the compiler give me a null error. I had attached the code on first post. Can you give me a hint about how to fix it please? – illunara Mar 08 '15 at 15:52
  • @illunara You need to attach an exact error message. – Ivan Aksamentov - Drop Mar 08 '15 at 19:21
  • Here you go, i guess the problem here is, i haven't set data to myClass yet ( and i don't know how to ) qrc:///mainControl.qml:192: TypeError: Cannot call method 'doSomething' of null – illunara Mar 09 '15 at 03:59