Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How do I get the context of a given component in C++?
Beriol last edited by
As that title says, how do I get the context of a certain component in C++?
I've got a QML signal connected to a C++ slot. Basically when the signal is emitted, QML passes a certain component as a QVariant to the C++ slot. From there, I need to access the context of that component in order to se a property.
I've seen that there's a method that does just that: QQmlEngine::contextForObject()
I tried it, but it gives me a weird error: 'QQmlContext::QQmlContext(constQQmlContext&)' is private
What is that about? Or am I doing something wrong?
The code right now is like this:
void MyClass::mySlot(QVariant myComponent)
QObject * obj = qvariant_cast<QObject *>(myComponent);
//Here I'm passing the object, and it gives me the error I mentioned QQmlContext context = QQmlEngine::contextForObject(obj); //Here with the context I would set a property... if it worked that is context->setContextProperty("something", &something);
I'm an idiot. I just noticed that contextForObject() returns a pointer, so the code I was using is wrong. It should be:
QQmlContext* context = QQmlEngine::contextForObject(obj);
Now I can get the context, but it won't let me set any properties in it. It gives me this error: QQmlContext: Cannot set property on internal context.
Are there any workarounds to that? How can I change the properties of a given context?
I have a feeling you are overcomplicating a simple thing.
First, pass a QObject pointer instead of a QVariant - you will avoid needless casting.
Second, the QObject itself has properties (see QObject::property() and QObject::metaObject() in QObject "documentation":http://qt-project.org/doc/qt-5/QObject.html). so you do not need the context from QML at all.
Beriol last edited by
Thanks for your anwser!
I didn't know I could pass directly a QObject from QML, good to know.
But regarding the second thing, I don't understand... how do those methods help me?
QObject::property() returns the property with the name passed as parameter, while QObject::metaObject() returns a QMetaObject and I'm not sure what to do with it...
I'll tell you a bit what I need to do, maybe it helps. Basically in QML I've got a list with results from searches: the user searches something, and the previous list (if there was) is replaced by a new one with the results.
In my application each view (as in component) gives the possibility to make searches, and every view has to show its own searches at any given time (let's just leave it at that, no need to go into details); so every view has a context that differs from the others in order to show different things in every view.
When the user searches something, I need to get the context of the view in which the search started, in order to change the model of the list of results so that it only changes in that given view, not in others.
So basically when the user searches, I need to set a property containing the new model of the list so that is accessible from the QML view that prompted the search. This way, since the list uses the model obtained from C++, whenever the property is redefined from C++ it will change in QML too. I don't know if I made myself clear...
From what I've seen, the QMetaObject that QObject::metaObject() doesn't let me define a new property (or redefine an old one) so that it would be accessible from QML, or maybe I'm missing something...