Results 1 to 4 of 4

Thread: QProcess...what's wrong

  1. #1
    Join Date
    Mar 2006
    Posts
    172
    Thanks
    30
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry QProcess...what's wrong

    I just can't get the output of "GNU locate" onto the Textedit Widget. Infact, the QByteArray is being detected empty!!

    if i start the process in startDetached(...) I can see the output on my terminal! but using only start(...) I can't seem to get it working!!

    Help pls...

    Qt Code:
    1. #include <QApplication>
    2. #include <QString>
    3. #include <QWidget>
    4. #include <QLabel>
    5. #include <QProcess>
    6. #include <QTextEdit>
    7. #include <QTextStream>
    8. #include <QByteArray>
    9.  
    10. int main(int argc,char *argv[])
    11. {
    12. QApplication app(argc,argv);
    13.  
    14. QWidget *w=new QWidget(0);
    15.  
    16. w->setGeometry(0,0,300,300);
    17.  
    18. QTextEdit *output=new QTextEdit(w);
    19.  
    20. QProcess locate;
    21. locate.setReadChannel(QProcess::StandardOutput);
    22. locate.start("locate *.mymenu");
    23.  
    24. QByteArray results=locate.readAllStandardOutput(); //readAll() too didn't work
    25.  
    26. if(results.isEmpty())
    27. {
    28. w->setWindowTitle("Sorry...doesn't work!");
    29. }
    30.  
    31. QTextStream data(&results);
    32.  
    33. output->setPlainText(data.readAll());
    34.  
    35. w->show();
    36.  
    37. return app.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 



    Thanks
    Last edited by nupul; 4th May 2006 at 07:48.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QProcess...what's wrong

    You'll have to either connect to signal QProcess::readyReadStandardOutput() to get notified when the data is available to be read, or more simply just wait for the whole process to finish before you read:
    Qt Code:
    1. locate.waitForFinished(-1);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    nupul (4th May 2006)

  4. #3
    Join Date
    Mar 2006
    Posts
    172
    Thanks
    30
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess...what's wrong

    IT WORKS!!!! how stupid of me to overlook this....even after having read the docs!!

    Now w.r.t to the first technique of using readyReadStandardOutput() it'll go something like this right?

    Qt Code:
    1. QObject::connect(locate,SIGNAL(readyReadStandardOutput()),myobject,SLOT(read_data()));
    To copy to clipboard, switch view to plain text mode 

    Is this correct?

    Nupul

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QProcess...what's wrong

    Yes, something like that.
    Just keep in mind that in some cases the signal might be emitted several times.

    1) process has output something and readyReadStandardOutput is emitted (while the process is still continued)
    2) you read the std output
    3) new data has arrived to the std output, the signal is emitted again..
    ...
    J-P Nurmi

Similar Threads

  1. Detect First QProcess finished in a group and kill other
    By Davidaino in forum Qt Programming
    Replies: 3
    Last Post: 11th July 2008, 12:53
  2. Replies: 2
    Last Post: 2nd June 2008, 08:45
  3. QProcess and Pipes
    By KaptainKarl in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2007, 23:11
  4. QProcess extremely slow on Windows?
    By Pepe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2007, 00:25
  5. Replies: 4
    Last Post: 27th July 2006, 11:13

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
  •  
Qt is a trademark of The Qt Company.