Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

How do I get the context of a given component in C++?

$
0
0
Hi! 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); }   Update: 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?

Viewing all articles
Browse latest Browse all 4972

Latest Images

Trending Articles



Latest Images