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. QObject's QThread from shared library
Qt 6.11 is out! See what's new in the release blog

QObject's QThread from shared library

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 147 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.
  • S Offline
    S Offline
    St.Stanislav
    wrote last edited by St.Stanislav
    #1

    Hello everyone! Questions about classes inherited from QObject in shared library and it's usage in the main application. Currently on Qt 6.5.0.

    I have a shared library with test class like this:

    class LIB_EXPORT Tester: public QObject {
        Q_OBJECT
    public:
        Tester(QObject* parent = nullptr):QObject(parent){};
        ~Tester() = default;
    };
    

    In main application I link the library and use it:

    Tester test;
    qDebug() << QThread::currentThread();
    qDebug() << test.thread();
    

    And output is like this:

    QThread(0x2b484981b50)
    QThread(0x2b4849c3990)
    

    So I cannot correctly work with this class, e.g. cannot move it to desired thread and so on. Even if in my main application I create derived class from Tester, it will be created in another thread.
    Is it correct behaviour? If so, what is the correct way to work with?

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

      This is working fine for me

      int main(int argc, char* argv[])
      {
          QApplication app(argc, argv);
      
          QObject obj;
          qDebug() << QThread::currentThread();
          qDebug() << obj.thread();
          return 0;
      }
      

      -->
      QThread(0x26c3915b030, name = "Qt mainThread")
      QThread(0x26c3915b030, name = "Qt mainThread")

      You must be doing something in your Tester ctor.

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

      S 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        This is working fine for me

        int main(int argc, char* argv[])
        {
            QApplication app(argc, argv);
        
            QObject obj;
            qDebug() << QThread::currentThread();
            qDebug() << obj.thread();
            return 0;
        }
        

        -->
        QThread(0x26c3915b030, name = "Qt mainThread")
        QThread(0x26c3915b030, name = "Qt mainThread")

        You must be doing something in your Tester ctor.

        S Offline
        S Offline
        St.Stanislav
        wrote last edited by
        #3

        @Christian-Ehrlicher said in QObject's QThread from shared library:

        You must be doing something in your Tester ctor.

        In the code above you can see that there is totally empty ctor :)
        But looks like that I have found the issue. Need to check it tomorrow.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          St.Stanislav
          wrote last edited by
          #4

          Still doesn't know, if this is a correct behaviour (can be logically explained, I mean), but the issue was quite simple. Somehow library's versions were mixed and debug shared library was linked to app's release version and vice versa. In this case QObjects from library (including inherited classes' objects) are created in a different QThread.

          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