Results 1 to 5 of 5

Thread: Qprocess for a prompted program

  1. #1
    Join Date
    May 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Qprocess for a prompted program

    I want to run an external program which is prompted (specifically gnuplot) and communicate with it writing on his prompt.
    I don't use writeToStdin() function because I'm using Qt4:
    Qt Code:
    1. process::process(QString progname)
    2. {
    3. progname="gnuplot";
    4. program=new QProcess;
    5. program->start(progname,QIODevice::ReadWrite);
    6. qDebug()<<program->isWritable();
    7. // program->execute(proc);
    8. qDebug()<<"paso execute";
    9.  
    10. program->write("plot sin(x) pause -1");
    11. qDebug()<<"paso plot";
    12. }
    To copy to clipboard, switch view to plain text mode 

    I added the start function because it wasn't writable without it.
    Last edited by muzgash; 10th May 2009 at 13:25.

  2. #2
    Join Date
    Jun 2007
    Location
    Cambridge, United Kingdom
    Posts
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qprocess for a prompted program

    Before you write to the gnuplot process you should wait until the process has started. Otherwise, the write comes to early and is ignored. The code should look as follows:

    program=new QProcess;
    program->start(progname,QIODevice::ReadWrite);
    if (!program.waitForStarted()) return;
    program->write("plot sin(x); pause -1;");
    Hope it helps,
    Burkhard

  3. #3
    Join Date
    May 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qprocess for a prompted program

    It doesn't work, I tried with the function and with no QProcess inheritance, now the class process inherits from QProcess and it doesn't work, but is the same sh*t.
    The code looks like this:

    Qt Code:
    1. process::process()
    2. {
    3.  
    4. start("gnuplot",QIODevice::ReadWrite);
    5.  
    6. qDebug()<<isOpen()<<"is open";
    7. qDebug()<<"pass on start";
    8.  
    9. // qDebug()<<waitForStarted()<<"wait4started";
    10. // write("plot sin(x);pause -1;");
    11. // qDebug()<<"pass on plot";
    12. // execute("plot sin(x);pause -1");
    13. // qDebug()<<readAll();
    14. }
    15.  
    16. void process::prun()
    17. {
    18. if(!waitForStarted()) return;
    19. write("plot sin(x); pause -1;");
    20. qDebug()<<"pass on plot";
    21.  
    22. // QProcess *gnuplot = new QProcess;
    23. //
    24. // gnuplot->start("gnuplot",QIODevice::ReadWrite);
    25. //
    26. // qDebug()<<gnuplot->isOpen()<<"is open";
    27. // qDebug()<<"pass on start";
    28. //
    29. // while(!gnuplot->waitForStarted()) return;
    30. // qDebug()<<gnuplot->waitForStarted()<<"wait4started";
    31. // gnuplot->write("plot sin(x);pause -1;");
    32. // qDebug()<<"pass on plot";
    33. //
    34. // qDebug()<<gnuplot->readAll();
    35.  
    36. }
    To copy to clipboard, switch view to plain text mode 
    and the output looks like this:

    true is open
    pass on start
    pass on plot
    QProcess: Destroyed while process is still running.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qprocess for a prompted program

    The last message suggests that your QProcess object is either going out of scope after being allocated on the stack, or being deliberately deleted after being allocated on the heap but before it has finished cleanly. I'd start by looking at the conditions that might destroy the QProcess object.

  5. #5
    Join Date
    May 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qprocess for a prompted program

    for the search engines...i had the same problem trying to execute gnuplot and writing commands to the QProcess. turns out what i was missing was newlines at the end of each command


    Qt Code:
    1. commands << "set datafile commentschars \";\"\n"
    2. << "set terminal png\n"
    3. << QString("set output \"").append(outfile).append("\"\n")
    4. << "set multiplot layout 2,1\n"
    5. ...
    6.  
    7. gnuplot.start("gnuplot", QIODevice::ReadWrite);
    8. foreach (QString cmd , commands) {
    9. gnuplot.write(cmd.toUtf8());
    10. }
    11. gnuplot.waitForFinished();
    To copy to clipboard, switch view to plain text mode 

    this code worked for me

Similar Threads

  1. Replies: 1
    Last Post: 1st December 2008, 21:02
  2. 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
  3. QProcess start a console program
    By Shawn in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2007, 12:38
  4. QProcess problem (Main program hangs)
    By sincnarf in forum Qt Programming
    Replies: 5
    Last Post: 11th October 2007, 09:26
  5. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19

Tags for this Thread

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.