Results 1 to 8 of 8

Thread: How to use QProcess to write multiple commands to external console based program.

  1. #1
    Join Date
    Jun 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question How to use QProcess to write multiple commands to external console based program.

    Hi,

    I'm using Qt to run inkscape.exe in shell mode. The idea is to process multiple files through inkscape. In shell mode, inkscape accepts commands from stdin. However I'm experiencing problems when I try to write more than one command to inkscape. Here's my code.

    Qt Code:
    1. int main(int argc, char* argv[]) {
    2. // create application
    3. QCoreApplication app(argc, argv);
    4. QTextStream out(stdout, QIODevice::Unbuffered | QIODevice::WriteOnly);
    5. QProcess inkex;
    6.  
    7. QStringList arguments;
    8. QString os_program = QDir::toNativeSeparators ("E:/_Inkscape/inkscape.exe");
    9. arguments << "--shell" << "--without-gui";
    10.  
    11. // start the program
    12. inkex.setProcessChannelMode(QProcess::ForwardedChannels);
    13. inkex.start (os_program, arguments, QIODevice::ReadWrite | QIODevice::Unbuffered);
    14. inkex.waitForStarted();
    15.  
    16. // query the version
    17. inkex.write("--version\n");
    18.  
    19. // export a file to png
    20. QString svgfile=QDir::toNativeSeparators("E:/_Development/Cpp/RxBuilder/test/test_twee.svg");
    21. QString pngfile=QDir::toNativeSeparators("E:/_Development/Cpp/RxBuilder/test/test_twee.png");
    22. QString cmd="--file=\"%1\" --export-png=\"%2\" --export-area=0:0:1042:768\n";
    23. cmd=cmd.arg(svgfile).arg(pngfile);
    24. out << "---" << endl;
    25. out << cmd << endl;
    26. out << "---" << endl;
    27. inkex.write(cmd.toAscii());
    28.  
    29. // quit the program
    30. inkex.write("quit\n");
    31. inkex.waitForBytesWritten();
    32. inkex.waitForFinished();
    33.  
    34. // exit
    35. app.quit();
    36. return 0;
    37. }
    To copy to clipboard, switch view to plain text mode 

    The expected result is that the version is displayed and an png-image is created based on the svg-image. However that line never is executed. When the application exits only the version is displayed.

    The commandline is not the problem. If I strip the call to:
    Qt Code:
    1. inkex.write("--version\n");
    To copy to clipboard, switch view to plain text mode 

    Inkscape suddenly responds by generating the image. Somehow the first command is actually blocking the execution of the second command. Any idea's on whats wrong here?

    Thx.
    Ruud

  2. #2
    Join Date
    May 2011
    Location
    Sweden
    Posts
    4
    Thanks
    2
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Question Re: How to use QProcess to write multiple commands to external console based program.

    I have the same problem. I want to disable network and then enable it.

    ifconfig ethPCI down
    ifconfig ethPCI up
    Here is my code:

    Qt Code:
    1. args<<"ethPCI"<<"down";
    2. QProcess *proc = new QProcess();
    3. process->start("ifconfig",args);
    4. process->waitForFinished();
    5. args<<"ethPCI"<<"up";
    6. process->start("ifconfig",args);
    To copy to clipboard, switch view to plain text mode 

    For some reason only the first command is executed which disables the interface. Second command is not executing.
    If I move the second command up then it works but I have to disable the network before enabling it otherwise its no use for me.

    Thanks for any help on this.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to use QProcess to write multiple commands to external console based program.

    You are not clearing "args" prior to executing line #6.
    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.


  4. The following user says thank you to wysota for this useful post:

    Fraz (13th September 2011)

  5. #4
    Join Date
    May 2011
    Location
    Sweden
    Posts
    4
    Thanks
    2
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Re: How to use QProcess to write multiple commands to external console based program.

    Quote Originally Posted by wysota View Post
    You are not clearing "args" prior to executing line #6.
    Thanks I did that and I think now the second command is also executing but now I have another problem. My program is unable to detect that the network is up until I rerun the program. Any ideas about it ?

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to use QProcess to write multiple commands to external console based program.

    How are you detecting the network?
    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.


  7. #6
    Join Date
    May 2011
    Location
    Sweden
    Posts
    4
    Thanks
    2
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Re: How to use QProcess to write multiple commands to external console based program.

    Quote Originally Posted by wysota View Post
    How are you detecting the network?
    The network is detected by the API provided by my GiGE Camera.
    The code for detecting Camera is executed after I run these shell commands. The code before these shell commands is only to initialize the QT GUI.
    After some experimentation I have also discovered that if I execute "ifconfig ethPCI down" using QProcess only then Camera / Network is not detected in the same session.
    If I rerun the program or check it outside the application camera is accessible without any problems.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to use QProcess to write multiple commands to external console based program.

    Ok, but probably it takes some time for your system to bring the connection up. The fact that "ifconfig" returns doesn't mean the network is ready for use. If you perform the check immediately after running the commands, you are creating a race condition. Why don't you use Bearer Management API available in Qt Mobility to detect connection changes?
    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.


  9. The following user says thank you to wysota for this useful post:

    Fraz (13th September 2011)

  10. #8
    Join Date
    May 2011
    Location
    Sweden
    Posts
    4
    Thanks
    2
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Re: How to use QProcess to write multiple commands to external console based program.

    Quote Originally Posted by wysota View Post
    Ok, but probably it takes some time for your system to bring the connection up. The fact that "ifconfig" returns doesn't mean the network is ready for use. If you perform the check immediately after running the commands, you are creating a race condition. Why don't you use Bearer Management API available in Qt Mobility to detect connection changes?
    OK I did not know about this API, I will give it a go and see if it solves the problem. Thanks


    Added after 1 21 minutes:


    Quote Originally Posted by wysota View Post
    Ok, but probably it takes some time for your system to bring the connection up. The fact that "ifconfig" returns doesn't mean the network is ready for use. If you perform the check immediately after running the commands, you are creating a race condition. Why don't you use Bearer Management API available in Qt Mobility to detect connection changes?
    I tried to use Qt Mobility but in my scenario it is useless. The reason I need to disable and enable network interface is that my network card sometimes (randomly) gets disabled by my Linux (Slackware 13.37) kernel.
    I tried to resolve that issue at Linux forums but as it seems its a bug in Slackware release or with my card driver. Anyway the only workaround is to disable and enable the card that makes it reusable without restarting the system.
    I tried doing the same thing with Qt API but it seems it can not disable the interface. It gives error "The requested operation is not supported by the system"

    Anyway! You were spot on ... it was a race condition because it takes some time for the network interface to become available, so I forced it to delay (sleep(5)) for 5 seconds and it solved my problem. Thanks once again.
    Last edited by Fraz; 13th September 2011 at 14:15.

Similar Threads

  1. running external console program by gui program
    By alireza.mixedreality in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2010, 19:05
  2. [QProcess] Calling console program
    By bgarisn in forum Newbie
    Replies: 3
    Last Post: 25th February 2010, 16:11
  3. Replies: 5
    Last Post: 17th July 2009, 13:17
  4. Replies: 1
    Last Post: 1st December 2008, 22:02
  5. QProcess start a console program
    By Shawn in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2007, 13:38

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.