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. Help with QThread design
Forum Updated to NodeBB v4.3 + New Features

Help with QThread design

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 1.6k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    JulienMaille
    wrote on last edited by
    #1

    Dear all, I recently had to change a class so it inherit from a QObject. Since this class was already inheriting from QThread and you can't inherit from 2 QObjects, I had to redesign it.

    @MyClass : public QThread
    {
    run(); // stuff that need to run in a thread
    void startLive() { start(); }
    void otherFunctions();
    };@
    ---->
    @
    MySuperClass : public QObject
    {};
    MyClass : public MySuperClass
    {
    QThread t;
    MyClass() { moveToThread(t); }
    void startLive() { t.start(); QMetaObject::invokeMethod(this, "run", Qt::QueuedConnection);}
    void run(); // stuff that need to run in a thread
    void otherFunctions();
    };@

    Problem: now my whole object resides in a different thread, which is unwanted. I can't call setParent() from my main thread anymore. How could I keep the fact that run() is actually called in a separate thread while the object lives in the main one?
    One option would be to create a sub object of MyClass and move it to the thread, but I'd like to know if there are other way to achieve that.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Something I don't quite understand, why would you need to inherit from both QObject and QThread since QThread is already a QObject ?

      Can you tell more about what you would like to achieve with your thread ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JulienMaille
        wrote on last edited by
        #3

        Ok maybe I simplified too much.
        I need MyClass to inherit from MySuperClass that inherits from QObject and I don't want MySuperClass to inherit from QThread.

        the thread is a loop that grabs images from a camera

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rondog
          wrote on last edited by
          #4

          I would create a new class of QThread (your 'MyClass'). The public functions would be to set or return information related to reading images from the camera.

          The thread emits 'start' and 'finished' so you can monitor this from the parent.

          @
          d_thread = new MyClass;
          connect(d_thread,SIGNAL(started(void)),this,SLOT(ThreadStarted(void)));
          connect(d_thread,SIGNAL(finished(void)),this,SLOT(ThreadFinished(void)));
          ...
          d_thread->clear_old_data();
          d_thread->set(... necessary information ...);
          d_thread->start(QThread::NormalPriority);

          ThreadStarted(void)
          {
          // reading data in process ...
          }

          ThreadFinished(void)
          {
          // done reading data ...
          data = d_thread->get_data(...);
          }

          @

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Ok, then, what should your MySuperClass do ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • J Offline
              J Offline
              JulienMaille
              wrote on last edited by
              #6

              Not a lot of stuff, but I will need a few signal and slots. Everything that is common to MyClass and MyOtherClass that will both inherit MySuperClass

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                [quote author="Julien M" date="1422869592"]Not a lot of stuff, but I will need a few signal and slots. Everything that is common to MyClass and MyOtherClass that will both inherit MySuperClass[/quote]Should those slots run in your main thread or worker thread?

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  I think you are confusing inheritance and composition. You need the latter, but you are trying to solve your issue using the former.

                  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