Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Troubles with multithreading
QtWS25 Last Chance

Troubles with multithreading

Scheduled Pinned Locked Moved Solved General and Desktop
multithreadingbegginerqmlc++ threadworkerthread
12 Posts 2 Posters 1.8k 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.
  • mrjjM mrjj

    Hi and welcome to the forums
    I think the issue is that QQmlApplicationEngine and your QObject need to be in same thread which they are not when you use moveToThread

    This poster however sounds like he found a workaround using
    a WorkerInterface design.

    https://forum.qt.io/topic/62073/qthread-qml

    L Offline
    L Offline
    luisfilipels
    wrote on last edited by
    #3

    @mrjj
    Thank you for your response. I tried my best to replicate the approach from the poster of the link you shared, but the "Non-existent attached object" persists, and what's worse, I now get "QQmlApplicationEngine failed to load component". Any leads to what may be wrong? I updated my repo with the new code.

    mrjjM 1 Reply Last reply
    0
    • L luisfilipels

      @mrjj
      Thank you for your response. I tried my best to replicate the approach from the poster of the link you shared, but the "Non-existent attached object" persists, and what's worse, I now get "QQmlApplicationEngine failed to load component". Any leads to what may be wrong? I updated my repo with the new code.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @luisfilipels

      Hi.
      And you reused his class WorkerInterface : public QObject
      that hold the thread and so on ?

      Did you try the whole sample ?

      L 1 Reply Last reply
      0
      • mrjjM mrjj

        @luisfilipels

        Hi.
        And you reused his class WorkerInterface : public QObject
        that hold the thread and so on ?

        Did you try the whole sample ?

        L Offline
        L Offline
        luisfilipels
        wrote on last edited by
        #5

        @mrjj
        I apologize for the late response.
        I do believe I have adapted most of the relevant code from the source you posted into my code, including the interface (it's in windowupdater.h), yet I still get the same errors as before.

        mrjjM 1 Reply Last reply
        0
        • L luisfilipels

          @mrjj
          I apologize for the late response.
          I do believe I have adapted most of the relevant code from the source you posted into my code, including the interface (it's in windowupdater.h), yet I still get the same errors as before.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #6

          @luisfilipels
          Hi
          A day is not late :)
          Hmm. Did you try his sample to test it actually does work still?
          (from 2015)

          Update:
          I tested the code and it gives no errors and seems to work fine.
          Here is the test project
          https://www.dropbox.com/s/itp2wycpiwk6l70/threadQML.zip?dl=0

          L 1 Reply Last reply
          3
          • mrjjM mrjj

            @luisfilipels
            Hi
            A day is not late :)
            Hmm. Did you try his sample to test it actually does work still?
            (from 2015)

            Update:
            I tested the code and it gives no errors and seems to work fine.
            Here is the test project
            https://www.dropbox.com/s/itp2wycpiwk6l70/threadQML.zip?dl=0

            L Offline
            L Offline
            luisfilipels
            wrote on last edited by luisfilipels
            #7

            @mrjj
            I just tried running the test project you sent, and it does seem to be working correctly my PC.
            I'm going to compare my code and his code to see what may be wrong. I'll update my post with what I find. Thank you for your help thus far.

            edit:
            I finally got it working! But with a problem: I had to redefine my interface as no longer being a singleton. I had to instantiate it from the QML code. Is there something wrong with how I created my singleton? :(

            mrjjM 1 Reply Last reply
            0
            • L luisfilipels

              @mrjj
              I just tried running the test project you sent, and it does seem to be working correctly my PC.
              I'm going to compare my code and his code to see what may be wrong. I'll update my post with what I find. Thank you for your help thus far.

              edit:
              I finally got it working! But with a problem: I had to redefine my interface as no longer being a singleton. I had to instantiate it from the QML code. Is there something wrong with how I created my singleton? :(

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @luisfilipels
              Hi
              Well as long as the singleton is created after QApplication ,
              it should not matter much even if used in a singleton pattern.
              Is the code from first link the actual code ?

              L 1 Reply Last reply
              1
              • mrjjM mrjj

                @luisfilipels
                Hi
                Well as long as the singleton is created after QApplication ,
                it should not matter much even if used in a singleton pattern.
                Is the code from first link the actual code ?

                L Offline
                L Offline
                luisfilipels
                wrote on last edited by
                #9

                @mrjj said in Troubles with multithreading:

                @luisfilipels
                Hi
                Well as long as the singleton is created after QApplication ,
                it should not matter much even if used in a singleton pattern.

                My main.cpp, as it was originally (using the singleton pattern):

                #include <QGuiApplication>
                #include <QQmlApplicationEngine>
                #include <QThread>
                #include <QObject>
                #include <QQmlContext>
                #include "windowupdater.h"
                
                int main(int argc, char *argv[])
                {
                    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                
                    QGuiApplication app(argc, argv);
                
                    QQmlApplicationEngine engine;
                
                    qDebug() << "Starting application" << endl;
                
                    UpdaterInterface::getInstance();
                
                    qmlRegisterSingletonType<UpdaterInterface>("threadtest", 1, 0, "Updater", &UpdaterInterface::UpdaterInterfaceProvider);
                
                    const QUrl url(QStringLiteral("qrc:/main.qml"));
                    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                                     &app, [url](QObject *obj, const QUrl &objUrl) {
                        if (!obj && url == objUrl)
                            QCoreApplication::exit(-1);
                    }, Qt::QueuedConnection);
                    engine.load(url);
                
                    return app.exec();
                }
                

                Is the code from first link the actual code ?

                Yes, it's the code as it was when I was using the singleton pattern. Here is the code as it is now, functional as it is: https://github.com/luisfilipels/testThreadQt/tree/noSingleton

                mrjjM 1 Reply Last reply
                0
                • L luisfilipels

                  @mrjj said in Troubles with multithreading:

                  @luisfilipels
                  Hi
                  Well as long as the singleton is created after QApplication ,
                  it should not matter much even if used in a singleton pattern.

                  My main.cpp, as it was originally (using the singleton pattern):

                  #include <QGuiApplication>
                  #include <QQmlApplicationEngine>
                  #include <QThread>
                  #include <QObject>
                  #include <QQmlContext>
                  #include "windowupdater.h"
                  
                  int main(int argc, char *argv[])
                  {
                      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                  
                      QGuiApplication app(argc, argv);
                  
                      QQmlApplicationEngine engine;
                  
                      qDebug() << "Starting application" << endl;
                  
                      UpdaterInterface::getInstance();
                  
                      qmlRegisterSingletonType<UpdaterInterface>("threadtest", 1, 0, "Updater", &UpdaterInterface::UpdaterInterfaceProvider);
                  
                      const QUrl url(QStringLiteral("qrc:/main.qml"));
                      QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                                       &app, [url](QObject *obj, const QUrl &objUrl) {
                          if (!obj && url == objUrl)
                              QCoreApplication::exit(-1);
                      }, Qt::QueuedConnection);
                      engine.load(url);
                  
                      return app.exec();
                  }
                  

                  Is the code from first link the actual code ?

                  Yes, it's the code as it was when I was using the singleton pattern. Here is the code as it is now, functional as it is: https://github.com/luisfilipels/testThreadQt/tree/noSingleton

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @luisfilipels
                  Hi
                  I think its the use of static that makes it unhappy as when you flag a member function with it
                  it becomes a class function and there is no "this" as such.
                  But im not sure if that was the actual problem.

                  Did it show some error when a singleton ?

                  L 1 Reply Last reply
                  2
                  • mrjjM mrjj

                    @luisfilipels
                    Hi
                    I think its the use of static that makes it unhappy as when you flag a member function with it
                    it becomes a class function and there is no "this" as such.
                    But im not sure if that was the actual problem.

                    Did it show some error when a singleton ?

                    L Offline
                    L Offline
                    luisfilipels
                    wrote on last edited by
                    #11

                    @mrjj said in Troubles with multithreading:

                    @luisfilipels
                    Hi
                    I think its the use of static that makes it unhappy as when you flag a member function with it
                    it becomes a class function and there is no "this" as such.

                    Do you mean the static constructor? But wouldn't it have to be static in order to be used as a singleton?

                    Did it show some error when a singleton ?

                    Yes, the ones I spoke about before. "Non-existent attached object" on the Updater.onUpdateGUI on main.qml, and "QQmlApplicationEngine failed to load component".

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      luisfilipels
                      wrote on last edited by
                      #12

                      Just replying to say I got it working 100% as I expected, while still registering my object as a singleton!
                      I just added a Connections element to my QML code, with the singleton object as its target. Thank you for your help, @mrjj

                      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