Results 1 to 5 of 5

Thread: Difference between QProcess and exec()

  1. #1
    Join Date
    Feb 2010
    Location
    Los Angeles
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Difference between QProcess and exec()

    All,

    I need to call a shell script from within my Qt 4.7 (on Linux) app and QProcess is giving me trouble.
    Here is the code that does not work - meaning it does execute the shell script but the environment is false:
    Qt Code:
    1. QProcess myProcess(this);
    2. myProcess->start(prog, args);
    To copy to clipboard, switch view to plain text mode 
    I tried setting "PATH" and some other env-variables via myProcess->setEnvironment() but with no effect. But this works fine:
    Qt Code:
    1. pid_t pID = fork();
    2. if (pID == 0)
    3. execl(prog,prog,args, (char *) 0);
    To copy to clipboard, switch view to plain text mode 
    Any suggestion on what is going on?

    Markus

  2. #2
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Difference between QProcess and exec()

    Can you give an example of how you are setting the environment (what are you passing to QProcess::setEnvironment())?

    What happens if you call:
    Qt Code:
    1. myProcess->setEnvironment(QProcess::systemEnvironment())
    To copy to clipboard, switch view to plain text mode 

    Also, the QProcess::setEnvironment function was deprecated in Qt 4.6 in favor of the QProcess::setProcessEnvironment(const QProcessEnvironment & environment). It should still work, but the trolls claim the latter is more efficient.
    Last edited by KShots; 18th November 2013 at 20:04.
    Life without passion is death in disguise

  3. #3
    Join Date
    Feb 2010
    Location
    Los Angeles
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Difference between QProcess and exec()

    I tried all variations of setting the environment but I does not make any difference.

  4. #4
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difference between QProcess and exec()

    Have you tried this:
    Qt Code:
    1. QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    2. env.insert("PATH", env.value("PATH") + ":" + settings->scriptsDirectory); // change this
    3. ...
    4. scriptProcess->setProcessEnvironment(env);
    5. scriptProcess->start(command, args);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2010
    Location
    Los Angeles
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Difference between QProcess and exec()

    I have a 1st clue, as soon as I start re-directing stdout my script fails. Code:
    Qt Code:
    1. int my_pipe[2];
    2. if(pipe(my_pipe) == -1)
    3. {
    4. fprintf(stderr, "Error creating pipe\n");
    5. }
    6.  
    7. pid_t pID = fork();
    8. if (pID == 0) // child
    9. {
    10. close(my_pipe[0]); // child doesn't read
    11. dup2(my_pipe[1], 1); // redirect stdout
    12. execl(prog,prog,args, (char *) 0);
    13. }
    To copy to clipboard, switch view to plain text mode 
    I think the problem might most likely be in the script than in Qt

Similar Threads

  1. Replies: 1
    Last Post: 3rd June 2013, 14:11
  2. Replies: 0
    Last Post: 23rd March 2013, 20:23
  3. Replies: 0
    Last Post: 26th August 2010, 11:44
  4. QProcess and kfmclient exec problem
    By ramazangirgin in forum Qt Programming
    Replies: 5
    Last Post: 6th June 2008, 09:31
  5. Sending a shortcut to an exec managed by QProcess ?
    By Ti_Thom in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2007, 18:31

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.