Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML property updating and qmlengine getting issue.
Forum Updated to NodeBB v4.3 + New Features

QML property updating and qmlengine getting issue.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
13 Posts 3 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jimmy Lee
    wrote on last edited by raven-worx
    #1

    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.

    raven-worxR 1 Reply Last reply
    0
    • J Jimmy Lee

      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.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Jimmy-Lee

      QQmlEngine::contextForObject(this)

      what is the this pointer?!

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jimmy Lee
        wrote on last edited by
        #3

        Thanks for your reply, I know I should replace this as the pointer of my thread class, but what should I insert here?

        raven-worxR 1 Reply Last reply
        0
        • J Jimmy Lee

          Thanks for your reply, I know I should replace this as the pointer of my thread class, but what should I insert here?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • J Offline
            J Offline
            Jimmy Lee
            wrote on last edited by
            #5

            I have tried declared a QObject* root in my class and set root near the QQmlApplicationEngine engine declared,

            MyClass root = engine.rootObjects().first();//in main.cpp
            

            then use this rootas pointer to

             QQmlContext *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)

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              @Jimmy-Lee said in QML property updating and qmlengine getting issue.:

              QQmlApplicationEngine

              1. You pass the address of QQmlApplicationEngine ? It is inherited from QQmlEngine.
              2. Define the method in your class like setMyEngine(QQmlApplicationEngine *eng);
              3. Call the method in main.cpp my object.setMyEngine(&engine).

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jimmy Lee
                wrote on last edited by
                #7

                Try your method but a Debug Error occurs:

                Debug Error! assert method.methodType()==QMetaMethod::Signal
                

                As my code shows following:
                main.h

                class 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!

                J 1 Reply Last reply
                0
                • J Jimmy Lee

                  Try your method but a Debug Error occurs:

                  Debug Error! assert method.methodType()==QMetaMethod::Signal
                  

                  As my code shows following:
                  main.h

                  class 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!

                  J Offline
                  J Offline
                  Jimmy Lee
                  wrote on last edited by
                  #8

                  @Jimmy-Lee
                  Has added this->

                  void MyThread::run()
                  {
                  for()
                  {
                       this->sub_engine->rootContext()->setContextProperty("row_data_contxt",m2);
                  
                  }
                  }
                  

                  But the Debug Error still occur once the run()

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    Jimmy Lee
                    wrote on last edited by
                    #9

                    It seems that setContextProperty can`t be execute from another thread in loop mode?

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jimmy Lee
                      wrote on last edited by
                      #10

                      In my latest code

                      void MyThread::run()
                      {
                      for()
                      {
                           this->sub_engine->rootContext()->setContextProperty("row_data_contxt",m2);
                           Sleep(250);
                      }
                      }
                      

                      Seems each times run at Sleep(25), it will occurs the Virtual C++ Debug error:

                      0_1548899433619_4c358159-48b0-454b-9cde-d116874f105c-image.png

                      What's the due to?

                      1 Reply Last reply
                      0
                      • dheerendraD Offline
                        dheerendraD Offline
                        dheerendra
                        Qt Champions 2022
                        wrote on last edited by dheerendra
                        #11

                        @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
                        @Community Service
                        Certified Qt Specialist
                        http://www.pthinks.com

                        J 1 Reply Last reply
                        0
                        • dheerendraD dheerendra

                          @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.

                          J Offline
                          J Offline
                          Jimmy Lee
                          wrote on last edited by
                          #12

                          @dheerendra
                          Sorry I've write what you mentioned in my codes, I didn't write them down here for convenient, my fault.

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            Jimmy Lee
                            wrote on last edited by
                            #13

                            For now, I've used Q_Property(QVariantMap x READ x) method to transmit variable to qml file, and engine.rootcontext()->setcontextproperty("",w1) to send my class instantiation w1 to 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!

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved