QProcess managed by a separate QThread



  • Hi all...

    I'm in trouble trying to manage a QProcess from a dedicated Qthread...

    I have followed what's described in this "this paper":http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong but it seems to me that what's valid for a generic QObject is not valid for a QProcess... in particular, it seems that moving the QProcess into the thread context this way:
    @
    FileConverter::FileConverter() : QObject()
    {
    convprocess = new QProcess();
    convthread = new QThread();
    convprocess->moveToThread(convthread);
    convthread->start();
    }
    @
    does not set the QProcess affinity permanently... as it seems to change when you start it some time later:
    @
    void FileConverter::startConvert(void)

    {
    convprocess->start("externalapp");
    // convprocess->execute("externalapp");
    }
    @
    as reported by this warning:
    @
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QProcess(0x8d6b1e8), parent's thread is QThread(0x8d4e358), current thread is QThread(0x882c148)
    @
    I know I can use also QProces's "Synchronous Process API" but... am I doing something completely wrong ?

    the next step was to connect qprocess signals to the FileConverter Object's slots...

    thanks a lot for any hints

           Giampaolo
    

    Edit: please use @ tags around code sections. It makes your posts that much more readable; Andre


  • Moderators

    QProcess provides a nice asynchronous API... it is not harder to use than the QThread API. So why would you even want to run a QThread to run a QProcess?



  • you are right... I think I misunderstand the whole QProcess concept...

        tnx
    
             Giampaolo


  • You cannot start a QProcess that lives within another thread, in other words QProcess::start() has to be always called from the thread QProcess is living in.



  • Hello lukas...

    so it could be possible to start the QProcess from a slot connected to the QThread::started signal ?

    thanks a lot

        Giampaolo


  • Hi all...

    problem solved... I follow Tobia's hints and removed the QThread object
    
           thanks 
    
               Giampaolo


  • [quote author="iw2lsi" date="1333456196"]
    So it could be possible to start the QProcess from a slot connected to the QThread::started signal?[/quote]

    No, because the QThread object still belongs to the main thread. It might be a bit confusing but the QThread object itself belongs to one thread (most probably the main thread), but executes the code of its QThread::run() member in another thread.

    If you want to start a QProcess that has been moved to another thread the most viable solution that comes to mind is a QProcess subclass which adds a slot to start itself, which then can be called through the threads event loop either using a connection or QMetaObject::invokeMethod(), so its code is actually executed in the thread the QProcess belongs to.

    Before digging into multithreaded applications with Qt make sure you have read the excellent article about "Threads, Events and QObjects":http://qt-project.org/wiki/ThreadsEventsQObjects.


Log in to reply
 

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