Results 1 to 9 of 9

Thread: QEventLoop with QProcess Questions

  1. #1
    Join Date
    Sep 2013
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QEventLoop with QProcess Questions

    Hello,

    I use several QProcess to start several daemons on Linux Os and "capture" there output in a slot when the signal readyRead() is emit by the QProcess.

    But sometimes there is a large amount of "payload" on the differents ouputs and some output datas seems to be lost.

    So to remedy to this issue, I use a thread with an infinite loop for each QProcess and it's seems to work, but I heard about the use of QEventLoop and maybe it could be a better solution :

    http://doc.qt.io/qt-5/qeventloop.html

    but I don't find any simple example that I'm able to understand and implement QProcess with it..

    In summary, I would like to be sure that' I don't miss any data ouput on something like that:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. (...)
    6. QString programFoo = "sh";
    7. QStringList argumentsFoo;
    8. argumentsFoo << "-c" << "./myFood";
    9. myDaemonFoo->start(programFoo, argumentsFoo);
    10.  
    11. connect(myDaemonFoo, SIGNAL(readyRead()), this, SLOT(readDaemonFooOutput()));
    12.  
    13. QString programBar = "sh";
    14. QStringList argumentsBar;
    15. argumentsBar << "-c" << "./myBard";
    16. myDaemonBar->start(programBar, argumentsBar);
    17.  
    18. connect(myDaemonBar, SIGNAL(readyRead()), this, SLOT(readDaemonBarOutput()));
    19. }
    20. void MainWindow::readDaemonFooOutput()
    21. {
    22. //display output in QtextEdit
    23. }
    24.  
    25. void MainWindow::readDaemonBAROutput()
    26. {
    27. //display output in QtextEdit
    28. }
    To copy to clipboard, switch view to plain text mode 
    My problem with using QThreads, is that I have to emit the result from the secondary thread to the main windows thread or/and I have to protect datas with a Qmutex and I'm afraid that I can still miss some data output from the daemon.

    If someone can help me or give me some advices,
    Thank you.

  2. #2
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QEventLoop with QProcess Questions

    Signal-Slot connections work across thread boundaries.


    on the differents ouputs and some output datas seems to be lost.
    That seems unlikely, probably there is a bug in your code.



    Consider using the more specific specific

    void readyReadStandardError()
    void readyReadStandardOutput()
    signals and show how you read the output in your slots.

  3. #3
    Join Date
    Sep 2013
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QEventLoop with QProcess Questions

    Thank you for your answer..

    So what you say is , if I use two thread A and B and "connect" them with a signal emit by A and process in a slot in B, I don't have to worry ?

    I'm doing some tests with Threads connected by signal-slot and it seems to be ok. My program is logging now and I'll check if it's really ok.

  4. #4
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QEventLoop with QProcess Questions

    So what you say is , if I use two thread A and B and "connect" them with a signal emit by A and process in a slot in B, I don't have to worry ?
    Yes. Look connectin types: https://doc.qt.io/qt-5/qt.html#ConnectionType-enum


    Default is AutoConnection

    (Default) If the receiver lives in the thread that emits the signal, Qt:irectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted.

  5. #5
    Join Date
    Sep 2013
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QEventLoop with QProcess Questions

    Thank you for your clarification.

    So as I didn't specified anything, it's the Qt::QueuedConnection which is used.

    I've got just one other question:

    what happens if a second signal is emited by thread A while the previous slot in thread B "launched" by the previous signal isn't finised to be executed?

  6. #6
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QEventLoop with QProcess Questions

    It will be "queued" (queued connection), so it will be executed after the previous signal was handled.

    Slots are executed within the context of the thread the slot lives in. All this is handled by QEventLoop in the background by Qt.

  7. #7
    Join Date
    Sep 2013
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QEventLoop with QProcess Questions

    ok, thank you, that's what I don't really well understand... how do QEventLoop operate.. how many slot can be queued? Should I use "directly" QEventLoop..etc..

  8. #8
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QEventLoop with QProcess Questions

    Ideally you never deal wit the event loop yourself. Every Qt-Thread automatically does this for you.

    I dont think there is a limit on the number of queued signals, no.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QEventLoop with QProcess Questions

    Ideally you never deal wit the event loop yourself. Every Qt-Thread automatically does this for you.
    Correct. The major exception to this general rule is if you have a thread (especially the GUI thread) in which you are doing some time-consuming computation. If the computation doesn't pause every so often to allow the event loop to run and process pending events, your GUI will "freeze" until the computation is finished.

    In this case, the usual solution is to do something like this:

    Qt Code:
    1. while ( !computationDone )
    2. {
    3. computeALittleBit();
    4. QCoreApplication::processEvents();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Without the call to processEvents() nothing happens to the GUI and it freezes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Problems with QEventLoop
    By marnando in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 25th February 2014, 20:19
  2. Replies: 1
    Last Post: 3rd June 2013, 14:11
  3. How to use QEventLoop?
    By MorrisLiang in forum Newbie
    Replies: 3
    Last Post: 13th May 2010, 17:23
  4. qprocess or qeventloop
    By knishaq in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2009, 11:14
  5. Questions about kill() and start() of QProcess
    By mp33919 in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2007, 14:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.