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. QThread for QQuickPaintedItem

QThread for QQuickPaintedItem

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 4 Posters 1.1k Views
  • 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    hello,

    usually when i have c++ object doing havy tasks and i need that object in QML ,
    i create another 'interface class' for it, in the interface classe i create a qthread and the timeconsuming object , i move the time consuming object to that thread, then i use time consuming methods through signal/slot machanisme by exposing my interface class to qml

    MyInterfaceClass obj; 
    QQmlContext *context = new QQmlContext(engine.rootContext());
    context->setContextProperty("myModel", &obj);
    

    but this time my worker object is a QQuickPaintedItem and i need to declare one in QML , so i need to

    qmlRegisterType<MyQQuickPaintedItemWithTimeConsumingMethods>("GCodeView", 1 ,0 , "Gcode");
    

    how to move this to a thread please ?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VinayBalajiRajputh
      wrote on last edited by
      #2

      @LeLev : Hi, actually I do not have a answer for your question but if you do not mind I would like to see your code, i.e. how u create the interface class and run on a thread? I have a similar use case.
      Thank you

      ODБOïO 1 Reply Last reply
      0
      • V VinayBalajiRajputh

        @LeLev : Hi, actually I do not have a answer for your question but if you do not mind I would like to see your code, i.e. how u create the interface class and run on a thread? I have a similar use case.
        Thank you

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @VinayBalajiRajputh like here

        1 Reply Last reply
        0
        • F Offline
          F Offline
          FKosmale
          wrote on last edited by
          #4

          I'd strongly warn against moving an item which is owned by the QML engine into another thread, as this would thread affinity issues with the internal QQmlData for the object, which is managed by the engine.

          Instead, I'd recommend that your MyQQuickPaintedItemWithTimeConsumingMethods has a worker object as a member or that it spawns a worker function in another thread (for intance via QtConcurrent::run).
          The expensive method in your class would then just call the corresponding method of the worker, and it would forward the worker's done signal to your classes users.
          If the data you work on is cheap to copy, I would just copy it for ease of programming; if the data is expensive to copy, you'll have to pass references to the worker and protect your data with locks.

          ODБOïO 1 Reply Last reply
          3
          • F FKosmale

            I'd strongly warn against moving an item which is owned by the QML engine into another thread, as this would thread affinity issues with the internal QQmlData for the object, which is managed by the engine.

            Instead, I'd recommend that your MyQQuickPaintedItemWithTimeConsumingMethods has a worker object as a member or that it spawns a worker function in another thread (for intance via QtConcurrent::run).
            The expensive method in your class would then just call the corresponding method of the worker, and it would forward the worker's done signal to your classes users.
            If the data you work on is cheap to copy, I would just copy it for ease of programming; if the data is expensive to copy, you'll have to pass references to the worker and protect your data with locks.

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by ODБOï
            #5

            @FKosmale hi, thank you for the suggestion

            i used QtConcurrent::run to execute my heavy methods

            void executeTimeConsumingMethod(){               
                    QtConcurrent::run([=]{ 
                        timeConsumingMethod(path);
                    });      
                }
            
            jsulmJ 1 Reply Last reply
            0
            • ODБOïO ODБOï

              @FKosmale hi, thank you for the suggestion

              i used QtConcurrent::run to execute my heavy methods

              void executeTimeConsumingMethod(){               
                      QtConcurrent::run([=]{ 
                          timeConsumingMethod(path);
                      });      
                  }
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @LeLev An important rule: never access GUI classes or QML elements outside of the GUI thread! This is not supported and will lead problems.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

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