Results 1 to 18 of 18

Thread: QProcess and the command line

  1. #1
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QProcess and the command line

    First to say - on linux, I dont have any problems. I use QProcess to start everything and it works.

    First trap - Windows. I can start anything, but for tools using the shell. QProcess::start does neither start cmd.exe (with complete path) nor any tool from cygwin (mc.exe, vi.exe, bash.bat). The only way to get a command line is to use QProgress::startDetached. But then I don't have any chance to set the environment of the shell...

    Second Trap - Mac. When my app is already started within the bash, and it starts another bash command, the new shell is opend within the window of the starting shell When the app is started from the dock, starting a command line does nothing at all. Neither QProcess::start nor QProcess::startDetached opens any window at all.

    Any suggesgion is welcome

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QProcess and the command line

    Could you try restating what your questions are? I can see descriptions of the problems but I don't know what you are really asking about.
    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.


  3. #3
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess and the command line

    Ok, after mentioning the symptoms, here's the short question: how can I start on the Mac a new bash shell with

    QProcess proc;
    QStringList args;
    proc->start("bash", args);

    ?

    (Same question for vi)

    And how can I do the same on Win32?

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess and the command line

    works fine under Windows
    Qt Code:
    1. QProcess::startDetached("cmd.exe");
    To copy to clipboard, switch view to plain text mode 
    did not test under other platforms, but I think it should work.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess and the command line

    Yes, it is on win32 the only way to start cmd. But how can I change the environment then?

    (On Mac, is does not work at all)

  6. #6
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Starting a Bash on a Mac

    Please, can someone tell me how I can start a new bash shell with QProcess?

  7. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QProcess and the command line

    really just a shell, or terminal like rxvt or konsole running a bash?

  8. #8
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess and the command line

    Set the environment before. The Environment of the process is inherited to its children (here: the command you want to start with QProcess)

    We're using this code to archive it (Works under Windows, Linux and OSX)

    Qt Code:
    1. // header
    2. enum EnvironmentSetOperationResult {
    3. CouldSetEvironmentVariable = 0,
    4. FailedToSetEnvironmentVariable = -1
    5. };
    6.  
    7. // cpp
    8.  
    9. #include <cstdlib>
    10.  
    11. #ifdef Q_WS_WIN
    12. // this is needed for the environment functions. Else the function will use unicode
    13. // and will not work with char*
    14. #undef UNICODE
    15. #include <windows.h>
    16. #endif
    17.  
    18. // [...]
    19.  
    20. void Environment::set(const QString& key, const QString& value) {
    21. // attention!
    22. //linux 0 == success, -1 == error
    23. //windows 0 == error, !0 == success
    24.  
    25. int num_error = FailedToSetEnvironmentVariable;
    26.  
    27. QByteArray key_ba = key.toLatin1();
    28. QByteArray value_ba = value.toLatin1();
    29.  
    30. #ifdef Q_WS_WIN
    31. if(value.isNull()) {
    32. num_error = SetEnvironmentVariableA((LPCSTR) key.toAscii().constData(), NULL);
    33. } else {
    34. num_error = SetEnvironmentVariableA((LPCSTR) key.toAscii().constData(), (LPCSTR) value.toAscii().constData());
    35. }
    36. num_error = (num_error == 0 ? FailedToSetEnvironmentVariable : CouldSetEvironmentVariable);
    37. #else
    38. const int overwrite_variable = 1;
    39. num_error = setenv(key_ba.constData(), value_ba.constData(), overwrite_variable);
    40. #endif
    41. if (num_error == FailedToSetEnvironmentVariable) {
    42. qDebug() << "Environment::set(): Could not set'" << key << "' to '" << value << "'";
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess and the command line

    Quote Originally Posted by caduel View Post
    really just a shell, or terminal like rxvt or konsole running a bash?
    I want to start the bash itself and a few instances of vi.

    Quote Originally Posted by nightghost View Post
    Set the environment before.
    And why not set it with QProcess?

  10. #10
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QProcess and the command line

    In order to start vi you need a window (assuming you don't intend to simulate a user by writing to stdin).

    So you will need to open some terminal application and run vi from there.

  11. The following user says thank you to caduel for this useful post:

    auba (19th May 2009)

  12. #11
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess and the command line

    And to start a graphical program like assistant? Is it possible to start it by commandline where the ".app/Contents/MacOS" part is some kind of autogenerated? I "only" have a new MacBook Pro, so I have no idea of the behaviour of the non-intel macs.

    ----

    This means starting a shell, I have the same difficulties as on windows, but there startDetached is working. Well.

    I found a swiss site with a small helper script.

    Maybe I check the existence of ~/.term, generate it and make it executable... or I check it into the repository, because it belongs to the maker suite anyway...

    thanks for that hint.
    Last edited by auba; 19th May 2009 at 10:12. Reason: ...clearify sentence...

  13. #12
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess and the command line

    Great. With the script I can start any bash command in a shell. Only when I close it, the window is left with "Prozess beendet". But I can live with it. Assistant and Designer are started with a symbolic QTDIR somewhere under "sources"... so everything runs now
    [Solved]

  14. #13
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QProcess and the command line on Mac

    Mmmh... it still does not really work. When I start makemk - the tool which wants to start a shell - from the command line, it works. I open a shell, I start vi, great.
    When I start makemk from the dock, it does not work. I neither get a shell nor a vi. Do I have to put any voodoo stuff around the call?

    Plus: when I say "exit" to close the shell again, the window stays open, and its title becomes "process stopped". Maybe thats just a setting from Terminal, but until now I did not find it

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QProcess and the command line on Mac

    If you want a terminal then start a terminal application. If you want a shell, then call bash (or whatever other shell you want directly) - you won't see it but it will be there. If something doesn't work then most likely it is a problem with relative paths.
    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.


  16. #15
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess and the command line

    hmm when I was making some application witch was calling latex to process the tex source files then I had QProcess object, and I just set some working dir (it was dir where my text files were) and used (pdflatex is a QProcess object, and pdflatex is a command line tool):
    Qt Code:
    1. pdflatex.setWorkingDirectory(currentProjectPath);
    2. pdflatex.start("pdflatex", QStringList() << project.rootFile());
    To copy to clipboard, switch view to plain text mode 
    It's working on Windows Vista, XP and linux (kubuntu with KDE 3.5 & 4.x).
    I did not have to call any bash, cmd or something and it is just working.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  17. #16
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess and the command line

    Quote Originally Posted by faldżip View Post
    It's working on Windows Vista, XP and linux
    Yes, for me too. This is a Mac-only problem

    Quote Originally Posted by wysota View Post
    If something doesn't work then most likely it is a problem with relative paths.
    Well, you could have been right. I called the script below simply with "term", and the path $QTHSRC/util is set in .profile. So if .profile would not have been read by the dock, it could not have find the term script. Now I call $QTHSRC/util/term, just to be sure. But the behaviour did not change, I can call assistant and designer, but not bash nor vi, except my program was started at the command line.

    Here is the script I call (from the mentioned page, thanks to Marc):
    Qt Code:
    1. !/bin/sh
    2. if [ "x-x" = x"$1" ]; then
    3. EXIT="; exit"; shift;
    4. fi
    5.  
    6. if [[ -d "$1" ]]; then
    7. WD=`cd "$1"; pwd`; shift;
    8. else
    9. WD="'`pwd`'";
    10. fi
    11.  
    12. COMMAND="cd $WD; $@"
    13. echo "$COMMAND $EXIT"
    14.  
    15. osascript 2>/dev/null <<EOF
    16. tell application "Terminal"
    17. activate
    18. do script with command "$COMMAND $EXIT"
    19. end tell
    20. EOF
    To copy to clipboard, switch view to plain text mode 

    it is called with "$QTHSRC/util/term -x <path> bash" and "$QTHSRC/util/term -x <path> vi". So - what's the difference?

    Quote Originally Posted by wysota View Post
    You won't see it but it will be there.
    I pushed the "Open Shell" button and called ps. It neither appeared a Terminal nor a bash command. And that is really strange...
    Edith says: doublecliking the term-scipt in the finder opens a terminal.

    Btw (little bit OT), does anybody know, where I can find the command line arguments of "Terminal"? I'd like to change the initial size and background color
    Last edited by auba; 27th May 2009 at 08:20.

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QProcess and the command line

    Quote Originally Posted by auba View Post
    Well, you could have been right. I called the script below simply with "term", and the path $QTHSRC/util is set in .profile. So if .profile would not have been read by the dock, it could not have find the term script. Now I call $QTHSRC/util/term, just to be sure. But the behaviour did not change, I can call assistant and designer, but not bash nor vi, except my program was started at the command line.
    You're still using a relative path, especially if $QTHSRC is undefined.

    I pushed the "Open Shell" button and called ps. It neither appeared a Terminal nor a bash command. And that is really strange...
    I'm not sure what you mean here... ps is a command placed in /bin/.
    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.


  19. #18
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess and the command line

    Quote Originally Posted by wysota View Post
    You're still using a relative path, especially if $QTHSRC is undefined.
    Thanks, that was the missing point: when $PATH is not set to $QTHSRC/util, why $QTHSRC itself should have been set
    cd /bin && sudo ln -s /Users/auba/sources/qhelpers/util/term and it works

    (... with ps I wanted to see if there was a bash opened in the background)

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.