Results 1 to 7 of 7

Thread: QProcess emits readyRead too late!

  1. #1
    Join Date
    Jun 2012
    Location
    Hamburg, DE
    Posts
    8
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QProcess emits readyRead too late!

    Hi,

    I wrote an appliaction for Android Debug Bridge (adb) to backup app data from an android device. But QProcess doesn't emit readRead signal, although adb gives an output:

    class.h:
    Qt Code:
    1. QProcess *process;
    To copy to clipboard, switch view to plain text mode 

    class.cpp:
    Qt Code:
    1. process = new QProcess(this);
    2. connect(process, SIGNAL(readyRead()), this, SLOT(readyRead()));
    3. connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
    4.  
    5. process->start("adb", QStringList() << "blah", QProcess::ReadOnly);
    To copy to clipboard, switch view to plain text mode 

    (...)

    Qt Code:
    1. void Class::readyRead() {
    2. qDebug() << "Class::readyRead()";
    3. }
    To copy to clipboard, switch view to plain text mode 

    In terminal the message is coming, than i confirm it on the device and than it's done. In QProcess the readRead signal is emitted after confirming on device! Why?

    Thanks!

    P.S.

    Qt Code:
    1. connect(process, SIGNAL(readyReadStandardOutput), this, SLOT(readyRead()));
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. process->start("adb", QStringList() << "blah", QProcess::ReadOnly | QIODevice::Text | QIODevice::Unbuffered);
    To copy to clipboard, switch view to plain text mode 

    doesn't work too!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess emits readyRead too late!

    Do you let your application process events often enough?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2012
    Location
    Hamburg, DE
    Posts
    8
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess emits readyRead too late!

    Will QProcess (adb) not running in a separate thread?

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QProcess emits readyRead too late!

    It will run in a separate process, not a separate thread. Are you using any of the QProcess::waitFor* methods by chance?

    Edit: I don't use Qt for Android, so perhaps the terminology process and thread are the same, I just don't know.

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess emits readyRead too late!

    @dynup

    Do not confuse the external process managed by the QProcess object with the thread in which this object lives. The QProcess object can only emit its readyRead() signal if the thread in which it lives processes its events. For instance, a minimal program could look like:

    Qt Code:
    1. int main(int argc, char **argv) {
    2. QCoreApplication app(argc, argv);
    3. ...
    4. MyObject o;
    5. ...
    6. connect(&p, SIGNAL(readyRead()), &o, SLOT(readAvailableDataAndDoSomethingWithIt()));
    7. p.start(...);
    8. // At this point, the external process may have started, but we are not yet handling any events; in particular, we will not pick up the notifications sent by the OS when the process writes to its output, and p will not emit readyRead()
    9. ...
    10. return app.exec(); // This starts the event loop; p will emit readyRead() when appropriate
    11. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2012
    Location
    Hamburg, DE
    Posts
    8
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess emits readyRead too late!

    Quote Originally Posted by jthomps View Post
    It will run in a separate process, not a separate thread. Are you using any of the QProcess::waitFor* methods by chance?
    ...with this methods the application freeze.

    Edit: I don't use Qt for Android, so perhaps the terminology process and thread are the same, I just don't know.
    It's not for Android, it's running on Linux!

    Quote Originally Posted by yeye_olive View Post
    Do not confuse the external process managed by the QProcess object with the thread in which this object lives. The QProcess object can only emit its readyRead() signal if the thread in which it lives processes its events.
    Ok, but how can I do this?

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess emits readyRead too late!

    Quote Originally Posted by dynup View Post
    Ok, but how can I do this?
    Well, I showed how in my previous post. In the simple program I wrote, the line
    Qt Code:
    1. app.exec();
    To copy to clipboard, switch view to plain text mode 
    runs the event loop and handles events until something asks it to quit. When the external process managed by p writes to its standard output, the OS notifies your program, the event loop picks this event, p emit readyRead(), and, thanks to the connection
    Qt Code:
    1. connect(&p, SIGNAL(readyRead()), &o, SLOT(readAvailableDataAndDoSomethingWithIt()));
    To copy to clipboard, switch view to plain text mode 
    the slot o.readAvailableDataAndDoSomethingWithIt() is run.

    You should read the Qt documentation about signals, slots, and event loops.

Similar Threads

  1. QProcess finishes but never emits finished signal
    By BettaUseYoNikes in forum Qt Programming
    Replies: 2
    Last Post: 3rd December 2011, 09:01
  2. QProcess, readyRead, QTextEdit
    By sgrant327 in forum Qt Programming
    Replies: 4
    Last Post: 1st February 2011, 17:49
  3. QProgressDialog shown too late
    By homerun4711 in forum Newbie
    Replies: 2
    Last Post: 17th January 2011, 18:22
  4. QFileDialog::getOpenFileName closing very late.
    By newtolinux in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2010, 13:36
  5. Replies: 8
    Last Post: 28th October 2010, 13:33

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.