Results 1 to 9 of 9

Thread: Problem with qprocess

  1. #1
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    1

    Question Problem with qprocess

    Hi guys,
    during my joyful experience with qt, i encountered a problems that it seems doesn't want to be handled! :
    I have some (external) console commands that I want to write some GUI for them. when running in a terminal like Konsole, these programs output sequence is :
    stderr (prints help message)
    stdout (asks what operation I would like to do usually using a menu)
    stdin (waiting for my input)

    as in

    Qt Code:
    1. $somecmd
    2. welcome to this program ......... (stderr)
    3. you can use this program this way "somecmd -[dfr] " (stderr)
    4.  
    5. what do you want to do? (stdout)
    6. 1 open a file (stdout)
    7. 2 write a file (stdout)
    8. 3 exit (stdout)
    9.  
    10. (stdin waiting for input)
    To copy to clipboard, switch view to plain text mode 

    in konsole or xterm everything is fine, but when using qprocess I can not get the menu which is shot out through stdout. So in the GUI, after running program, it prints its stderr messages and without printing stdout menu, is waiting for input. I have tried various methods (merged channels, using DupStderr in q3process, using pipes, ...) but never could have stdout messages which comes before stdin. if I closeWriteChannel() in slot which reads stderr, the menu is printed but this time I dont have accessibility to stdin and program continues using its defaults.

    I think the matter is opening stdin closes stdout channel(which seems reasonable) but how can I have those buffered stdouts? Do you know how konsole or other emulators manage this? Do I need to subclass qprocess? Defining child processes may be helpful? how?

    Any help, hint or suggestion is quite appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qprocess

    How do you read data from QProcess?

  3. #3
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    1

    Default Re: Problem with qprocess

    I have tried these ones:

    Qt Code:
    1. m_process->setCommunication( Q3Process::Stdin | Q3Process::Stdout | Q3Process::Stderr | Q3Process::DupStderr);
    2. m_process->start()
    3. while(m_process->canReadLineStdout() )
    4. qDebug()<<m_process->readLineStdout();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. connect( m_process, SIGNAL(readyReadStdout()), this, SLOT(readStandardOutput()) )
    2. m_process->setCommunication( Q3Process::Stdin | Q3Process::Stdout | Q3Process::Stderr | Q3Process::DupStderr);
    3. m_process->start();
    4. ....
    5. qDebug()<<m_process-> readStdout() [in readStandardOutput() slot]
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. m_process = new QProcess(this);
    2. m_process->setProcessChannelMode(QProcess::MergedChannels);
    3. connect(m_process, SIGNAL(readyReadStandardError()), this, SLOT(readStandardError()));
    4. connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readStandardOutput()));
    5. startProcess(m_progname);
    6. ...
    7. qDebug()<< QString(m_process->readAllStandardError()); [readStandardError() slot]
    8. ...
    9. qDebug()<< QString(m_process->readAllStandardOutput()); [readStandardOutput() slot]
    10. ...
    11.  
    12. [as I said if I close write channel in readStandardError() slot, I will get those lines ]
    To copy to clipboard, switch view to plain text mode 

    I even tried to redirect outputs to files, but again no success in getting those lines before stdin prompt. Am I doing something wrong?

    I have reviewed some codes of others (both qt3 and qt4) especially codes of IDEs but no success. I would be happy even if you point me some where (code, thread..) to learn more.
    I tried Google and Koders, but no success again.

    Besides, is there any non-qt work-around for this problem? Do you know some pure C++ console like code, which is wrapped in qt?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qprocess

    Quote Originally Posted by resal View Post
    m_process->setProcessChannelMode(QProcess::MergedChannels) ;
    Shouldn't you use QProcess::SeparateChannels here (i.e. the default mode)?

    What do the "out" and "err" files contain after you do the following?
    $ echo "3" > in # or whatever command is for "do nothing and exit"
    $ thatcmd < in > out 2> err

  5. #5
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    1

    Default Re: Problem with qprocess

    Quote Originally Posted by jacek View Post
    Shouldn't you use QProcess::SeparateChannels here (i.e. the default mode)?
    I tried separate channels first, but to make stdout unbuffered, I used merged channel too. no success in both.

    Quote Originally Posted by jacek View Post
    What do the "out" and "err" files contain after you do the following?
    what that they expected to have. stderr messages in err file, and stdout menu in out file.[ My fault: when I said no success in getting those lines in redirections, I meant that I couldn't have those lines BEFORE writing to stdin. The problem is to have menu then providing input based on this menu.]
    Last edited by resal; 27th August 2007 at 01:21.

  6. #6
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    1

    Default Re: Problem with qprocess

    Do you know any (not-very-complicated) (especially in Qt4) application with similar functionality? I mean an application which is a GUI that runs some commands AND communicates with those commands while they are still running(i.e. it doesn't wait for commands to finish and then gets the output; rather it gets the messages of commands during execution, displays the messages to user and then writes user options to command stdin). I reviewed konsole code, but got lost in (kde-related) classes....

    thanks
    Last edited by resal; 27th August 2007 at 10:13. Reason: updated contents

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qprocess

    Quote Originally Posted by resal View Post
    what that they expected to have. stderr messages in err file, and stdout menu in out file.
    OK, so at least that application isn't playing any tricks by detecting whether the input comes from a file or a terminal.

    There is no way to control the buffers through QProcess, but maybe reimplementing QProcess::setupChildProcess() will help. You might try to alter the buffer size with setbuf, but I'm not sure if it's going to work (I think I've tried it to solve a similar problem, but without a success).

    Quote Originally Posted by resal View Post
    Do you know any (not-very-complicated) (especially in Qt4) application with similar functionality?
    Unfortunately, I don't.

    Quote Originally Posted by resal View Post
    I reviewed konsole code, but got lost in (kde-related) classes....
    kconsole doesn't use pure QProcess, but a K3Process subclass. The most important difference is that KDE allows you to use a pty device to communicate with the child process.

    Here are the relevant sources:
    http://websvn.kde.org/trunk/KDE/kdeb....h?view=markup
    http://websvn.kde.org/trunk/KDE/kdeb...pp?view=markup
    http://api.kde.org/4.0-api/kdelibs-a...rocess_8h.html
    http://api.kde.org/4.0-api/kdelibs-a...classKPty.html

  8. The following user says thank you to jacek for this useful post:

    resal (28th August 2007)

  9. #8
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    1

    Default Re: Problem with qprocess

    Thank you jacek.

    I have reviewed them before. As you said the trick in konsole is using kpty device.

    Quote Originally Posted by jacek View Post
    There is no way to control the buffers through QProcess, but maybe reimplementing QProcess::setupChildProcess() will help. You might try to alter the buffer size with setbuf, but I'm not sure if it's going to work (I think I've tried it to solve a similar problem, but without a success).
    I think this is the case. I 've played with setupChild Process() but I couldn't figured it out how to use it. I found these links though :
    http://labs.trolltech.com/blogs/2006...with-qprocess/
    http://www.koders.com/default.aspx?s...arch&la=*&li=*
    http://www.koders.com/cpp/fidCB4E6B7...upchildprocess

    The first one, I think, is the solution. I tried to find some example of its usage, so I search koders, which didn't help me a lot.

    Can anyone write an example code about how to use the method that first link suggest?

    Thanks in advance.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qprocess

    Quote Originally Posted by resal View Post
    I think this is the case. I 've played with setupChild Process() but I couldn't figured it out how to use it.
    You have to create a QProcess subclass and use that new class instead.
    Qt Code:
    1. class UnbufferedProcess : public QProcess
    2. {
    3. protected:
    4. void setupChildProcess()
    5. {
    6. ::setbuf( stdout, 0 );
    7. }
    8. };
    To copy to clipboard, switch view to plain text mode 
    But as I said, I'm not sure if it's going to help.

    Quote Originally Posted by resal View Post
    The first one, I think, is the solution. I tried to find some example of its usage, so I search koders, which didn't help me a lot.
    You use it just like QProcess, but I'm not sure if it solves your problem. I think that this class is useful if the process you want to start insists on reading from the terminal. Nevertheless, try it.

Similar Threads

  1. QThread and QProcess problem
    By codebehind in forum Qt Programming
    Replies: 13
    Last Post: 7th August 2007, 08:11
  2. Quoting problem with QProcess
    By the_bis in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2006, 11:24
  3. problem with qprocess
    By deekayt in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2006, 13:30
  4. QProcess problem in accessing stdout
    By aruna in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2006, 17:56
  5. QProcess problem with windows batch file
    By bood in forum Qt Programming
    Replies: 11
    Last Post: 6th January 2006, 08:08

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.