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.
  • M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 1 Jan 2020, 20:40 last edited by mrjj 1 Jan 2020, 20:40
    #2

    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 1 Reply Last reply 1 Jan 2020, 22:54
    1
    • M mrjj
      1 Jan 2020, 20:40

      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 1 Jan 2020, 22:54 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.

      M 1 Reply Last reply 2 Jan 2020, 11:12
      0
      • L luisfilipels
        1 Jan 2020, 22:54

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

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 2 Jan 2020, 11:12 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 3 Jan 2020, 22:22
        0
        • M mrjj
          2 Jan 2020, 11:12

          @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 3 Jan 2020, 22:22 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.

          M 1 Reply Last reply 3 Jan 2020, 22:34
          0
          • L luisfilipels
            3 Jan 2020, 22:22

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

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 3 Jan 2020, 22:34 last edited by mrjj 1 Apr 2020, 14:46
            #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 4 Jan 2020, 20:41
            3
            • M mrjj
              3 Jan 2020, 22:34

              @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 4 Jan 2020, 20:41 last edited by luisfilipels 1 Apr 2020, 20:56
              #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? :(

              M 1 Reply Last reply 4 Jan 2020, 21:17
              0
              • L luisfilipels
                4 Jan 2020, 20:41

                @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? :(

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 4 Jan 2020, 21:17 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 4 Jan 2020, 23:13
                1
                • M mrjj
                  4 Jan 2020, 21:17

                  @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 4 Jan 2020, 23:13 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

                  M 1 Reply Last reply 4 Jan 2020, 23:30
                  0
                  • L luisfilipels
                    4 Jan 2020, 23:13

                    @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

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 4 Jan 2020, 23:30 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 5 Jan 2020, 03:24
                    2
                    • M mrjj
                      4 Jan 2020, 23:30

                      @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 5 Jan 2020, 03:24 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 6 Jan 2020, 22:54 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

                        11/12

                        5 Jan 2020, 03:24

                        • Login

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