Results 1 to 18 of 18

Thread: QProcess and the command line

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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]

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
  •  
Qt is a trademark of The Qt Company.