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. Illegal attempt to connectthe class that is in a different thread that the QML engine
QtWS25 Last Chance

Illegal attempt to connectthe class that is in a different thread that the QML engine

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 298 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
    MaximBozek
    wrote on last edited by
    #1

    Hi,

    I have this class to handle incoming CAN Frames. I want to emit these frames to be printed in a TextArea, but I am getting this error:

    QML debugging is enabled. Only use this in a safe environment.
    QML thread running
    Connected to CAN interface: vcan0
    WARNING: QApplication was not created in the main() thread.
    qrc:/Depoortere_Visualization_2/main.qml:220:9: QML Connections: Implicitly defined onFoo properties in Connections are deprecated. Use this syntax instead: function onFoo(<arguments>) { ... }
    QQmlEngine: Illegal attempt to connect to CANHandler(0x7ffe0ef65080) that is in a different thread than the QML engine QQmlApplicationEngine(0x7fc4b6d42ac0.
    

    I have tried numerous thing to get this working, but ended with the same error each time. I am running this in separate threads to be able to continuously read incoming CAN frames. Anybody have any idea on a fix.

    This is my main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <iostream>
    #include <thread>
    #include "CANHandler.h"
    #include "TranslationHandler.h"
    
    int qmlFunction(int argc, char *argv[], CANHandler *canHandler) {
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
    
        TranslationHandler transHandler;
        engine.rootContext()->setContextProperty("transHandler", &transHandler);
    
        engine.rootContext()->setContextProperty("canHandler", canHandler);
    
        const QUrl url("qrc:/Depoortere_Visualization_2/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);
    
        if (engine.rootObjects().isEmpty()) {
            std::cerr << "QML failed to load." << std::endl;
            return -1;
        }
    
        return app.exec();
    }
    
    int main(int argc, char *argv[])
    {
        CANHandler canHandler;
    
        std::thread canThread([&]() {
            QObject::connect(&canHandler, &CANHandler::newFrameReceived, [&](const QString &frame) {
                std::cout << frame.toStdString() << std::endl;
            });
            canHandler.mainFunction();
        });
    
        std::thread qmlThread(qmlFunction, argc, argv, &canHandler);
    
        if (canThread.joinable()) {
            std::cout << "CAN thread running" << std::endl;
            canThread.join();
        }
    
        if (qmlThread.joinable()) {
            std::cout << "QML thread running" << std::endl;
            qmlThread.join();
        }
    
        return 0;
    }
    
    1 Reply Last reply
    0
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      alt text


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      M Pl45m4P 2 Replies Last reply
      4
      • J.HilkJ J.Hilk

        alt text

        M Offline
        M Offline
        MaximBozek
        wrote on last edited by
        #3

        @J-Hilk Thanks for the help...

        J.HilkJ 1 Reply Last reply
        0
        • M MaximBozek

          @J-Hilk Thanks for the help...

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @MaximBozek said in Illegal attempt to connectthe class that is in a different thread that the QML engine:

          @J-Hilk Thanks for the help...

          You're welcome.

          It might look silly, because its answered as a meme, but its generally the problem and solution.

          your CANHandler is created as the first thing on the stack. Way later the QGuiApplication is created on the stack of a different thread even.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • M Offline
            M Offline
            MaximBozek
            wrote on last edited by
            #5

            Yes, but I want these running in different threads. I read and saw some material on Signals and slots, but I haven't really figured out how it works completaly. I think it should be possible with signals and slots.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Why can't you simply do what @J-Hilk told you to do - create a QGuiApplication first.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                alt text

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #7

                @J-Hilk

                This masterpiece deserves an extra medal 🤣


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                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