QML property updating and qmlengine getting issue.
-
I am trying to update my qml property by "engine2->rootContext()->setContextProperty("row_data_contxt",m2);" in a sub-thread, which means not in main.cpp, and I used
QQmlContext *currentContext = QQmlEngine::contextForObject(this); QQmlEngine *engine2 = currentContext->engine();to get the engine which main.cpp file launched, but it failed as returning a null pointer.
-
I am trying to update my qml property by "engine2->rootContext()->setContextProperty("row_data_contxt",m2);" in a sub-thread, which means not in main.cpp, and I used
QQmlContext *currentContext = QQmlEngine::contextForObject(this); QQmlEngine *engine2 = currentContext->engine();to get the engine which main.cpp file launched, but it failed as returning a null pointer.
-
Thanks for your reply, I know I should replace
thisas the pointer of my thread class, but what should I insert here?@Jimmy-Lee
you actually should insert a pointer to an item which "lives" in a QML engine (like an item).
Thats the purpose of this method after all.You may also take a look at qmlEngine() method.
-
I have tried declared a
QObject* rootin my class and setrootnear theQQmlApplicationEngine enginedeclared,MyClass root = engine.rootObjects().first();//in main.cppthen use this
rootas pointer toQQmlContext *currentContext = QQmlEngine::contextForObject(root); //upline:"read access violation exception" QQmlEngine *engine2 = currentContext->engine();What 's the right way to pass the
enginefrom main.cpp to other class?
(I've read the qmlengine() but what I use is QQmlApplicationEngine) -
@Jimmy-Lee said in QML property updating and qmlengine getting issue.:
QQmlApplicationEngine
- You pass the address of QQmlApplicationEngine ? It is inherited from QQmlEngine.
- Define the method in your class like setMyEngine(QQmlApplicationEngine *eng);
- Call the method in main.cpp my object.setMyEngine(&engine).
-
Try your method but a Debug Error occurs:
Debug Error! assert method.methodType()==QMetaMethod::SignalAs my code shows following:
main.hclass MyThread : public QObject { Q_OBJECT public: MyThread(QObject* parent = nullptr); ~MyThread(); QQmlApplicationEngine* sub_engine; void setEngineAddress(QQmlApplicationEngine* en) { sub_engine = en; } };main.cpp
int main() { MyThread *w1; QQmlApplicationEngine engine; w1->setEngineAddress(&engine); w1->start(); } void MyThread::run() { sub_engine->rootContext()->setContextProperty("row_data_contxt",m2); }Is there something wrong? Thanks a lot!
-
Try your method but a Debug Error occurs:
Debug Error! assert method.methodType()==QMetaMethod::SignalAs my code shows following:
main.hclass MyThread : public QObject { Q_OBJECT public: MyThread(QObject* parent = nullptr); ~MyThread(); QQmlApplicationEngine* sub_engine; void setEngineAddress(QQmlApplicationEngine* en) { sub_engine = en; } };main.cpp
int main() { MyThread *w1; QQmlApplicationEngine engine; w1->setEngineAddress(&engine); w1->start(); } void MyThread::run() { sub_engine->rootContext()->setContextProperty("row_data_contxt",m2); }Is there something wrong? Thanks a lot!
@Jimmy-Lee
Has addedthis->void MyThread::run() { for() { this->sub_engine->rootContext()->setContextProperty("row_data_contxt",m2); } }But the Debug Error still occur once the run()
-
@Jimmy-Lee said in QML property updating and qmlengine getting issue.:
MyThread *w1;
You are not creating the QApplication object itself in main.cpp ? Did you allocate the memory for W1 ? What is m2 object ? Did you allocate memory ?
Please do the default project in Qt Creator and try with modifications. Don't remove standard code like Application object, exec() etc.
-
@Jimmy-Lee said in QML property updating and qmlengine getting issue.:
MyThread *w1;
You are not creating the QApplication object itself in main.cpp ? Did you allocate the memory for W1 ? What is m2 object ? Did you allocate memory ?
Please do the default project in Qt Creator and try with modifications. Don't remove standard code like Application object, exec() etc.
@dheerendra
Sorry I've write what you mentioned in my codes, I didn't write them down here for convenient, my fault. -
For now, I've used
Q_Property(QVariantMap x READ x)method to transmit variable to qml file, andengine.rootcontext()->setcontextproperty("",w1)to send my class instantiationw1to qml file, which satisfy my requirement.inspired by: https://forum.qt.io/topic/26464/solved-q_property-qstring-linking-qml-and-c-giving-undefined-error/2
Thanks!
