Help with QThread design



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


  • Lifetime Qt Champion

    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 ?



  • 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



  • 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(...);
    }

    @


  • Lifetime Qt Champion

    Ok, then, what should your MySuperClass do ?



  • 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


  • Moderators

    [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?



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.