Results 1 to 20 of 33

Thread: Both a command line and GUI application at the same time?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2010
    Posts
    319
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 14 Times in 12 Posts

    Default Re: Both a command line and GUI application at the same time?

    high_flyer, you are correct and I have got things to work the way I want on Linux (and most likely on Mac OS X too) using the following code:

    Qt Code:
    1. int main(int pArgc, char *pArgv[])
    2. {
    3. QtSingleApplication app(pArgc, pArgv);
    4.  
    5. // Handle the command line options, if any
    6.  
    7. QxtCommandOptions options;
    8.  
    9. options.add("help", "Show this help text");
    10. options.alias("help", "h");
    11.  
    12. options.parse(app.arguments());
    13.  
    14. if(options.count("help") || options.showUnrecognizedWarning())
    15. {
    16. options.showUsage();
    17.  
    18. return -1;
    19. }
    20.  
    21. ...
    22.  
    23. // Execute the application
    24.  
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    It's just that on Windows, a GUI application cannot directly write to the console (unless your application is a pure console application which mine clearly isn't) and

    Qt Code:
    1. options.showUsage();
    To copy to clipboard, switch view to plain text mode 
    above will therefore never output anything while it does on Linux.

    Now, I had a quick look at the Windows API and it would seem that one might be able to do work around this issue by attaching a console to the GUI application:

    Qt Code:
    1. #ifdef Q_WS_WIN
    2. #define _WIN32_WINNT 0x0501 // I.e. Windows XP and higher
    3.  
    4. #include <windows.h>
    5. #endif
    6.  
    7. ...
    8.  
    9. int main(int pArgc, char *pArgv[])
    10. {
    11. ...
    12.  
    13. if(options.count("help") || options.showUnrecognizedWarning())
    14. {
    15. #ifdef Q_WS_WIN
    16. AttachConsole(ATTACH_PARENT_PROCESS);
    17.  
    18. // Some calls to WriteConsole to simulate the call to options.showUsage() which
    19. // works perfectly fine on Linux but not on bloody Windows!
    20. #else
    21. options.showUsage();
    22. #endif
    23.  
    24. return -1;
    25. }
    26.  
    27. ...
    28. }
    To copy to clipboard, switch view to plain text mode 
    The problem with the above is that it's really ugly and very much looks like a big hack to get around Windows' stupid limitation.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Both a command line and GUI application at the same time?

    If you are using MSVS just set Linker->system->console. - and it will add a console window to which your application can output.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2006
    Location
    Mexico City
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Both a command line and GUI application at the same time?

    Also you can add console to the CONFIG line in your project file. No need to make simple things complex...

  4. #4
    Join Date
    Mar 2010
    Posts
    319
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 14 Times in 12 Posts

    Default Re: Both a command line and GUI application at the same time?

    Sorry, I guess I should have made it clear that I do NOT want the console window to be visible at all times, ONLY if there is a need for it, i.e. when arguments passed to my application require output to the console. (Otherwise, I don't use QMake, but CMake and this not with MSVS but with MinGW )

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Both a command line and GUI application at the same time?

    If what you need is only an output window, then just make one using widgets - I don't know if its possible to call a console window in runtime, and associating it with your application for output in any cross platform way.
    For startup arguments you will have to have console before your application starts any way.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Mar 2010
    Posts
    319
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 14 Times in 12 Posts

    Default Re: Both a command line and GUI application at the same time?

    Quote Originally Posted by high_flyer View Post
    If what you need is only an output window, then just make one using widgets - I don't know if its possible to call a console window in runtime, and associating it with your application for output in any cross platform way.
    It's not only an output window that I need. I really need access to the console. On Linux, for example, one might want to redirect the output to a file or something. If I was to create an output window, I couldn't do that.

    Quote Originally Posted by high_flyer View Post
    For startup arguments you will have to have console before your application starts any way.
    Not sure what you mean by this...

  7. #7
    Join Date
    Mar 2006
    Location
    Mexico City
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Both a command line and GUI application at the same time?

    Again, you can hide() your main window, so there is no GUI showing up, and then you can use a QProcess to execute the command processor of your OS. You can use readData() and writeData() for your IO.

    Now you can control your GUI, use objects already written, open a console if you need to, and read or write from that process. Is this an option to solve your problem?

  8. #8
    Join Date
    Mar 2010
    Posts
    319
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 14 Times in 12 Posts

    Default Re: Both a command line and GUI application at the same time?

    Quote Originally Posted by arnaiz View Post
    Again, you can hide() your main window, so there is no GUI showing up, and then you can use a QProcess to execute the command processor of your OS. You can use readData() and writeData() for your IO.

    Now you can control your GUI, use objects already written, open a console if you need to, and read or write from that process. Is this an option to solve your problem?
    I get the feeling that we are not understanding one another. As I said, everything works as I expect on Linux and Mac OS X (I have just checked), so let me explain a bit more what is 'wrong' with Windows.

    Ok, say that you open a command prompt window. From there, if I enter the name of my application without any argument, then I want the GUI to show up (easy, since it's already the way it works):

    C:\MyApplicationDirectory>MyApplication

    C:\MyApplicationDirectory>
    Now, say that I add some arguments. This time round, I want my application to behave as a pure console application, so that I can do whatever I want with it (e.g. redirect the output to a file):

    C:\MyApplicationDirectory>MyApplication /i myInputFile
    MyApplication is going to proceed using myInputFile as an input file
    ...

    C:\MyApplicationDirectory>
    Now, as I have said in a previous reply, I can use AttachConsole() and WriteConsole() to 'mimick' the above output, but 1) it's a hack and 2) I can't get the right kind of output (and I am not sure whether I could redirect to a file; I haven't tried) since when starting a GUI application from the command line, Windows goes back to the prompt straightaway, so if anything using AttachConsole() and WriteConsole() I could get:

    C:\MyApplicationDirectory>MyApplication /i myInputFile
    C:\MyApplicationDirectory>
    MyApplication is going to proceed using myInputFile as an input file
    ...
    which is clearly not professional...

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Both a command line and GUI application at the same time?

    It's not only an output window that I need. I really need access to the console.
    I don't understand how you mean that.
    Lets say you have an open console, and you start you application with arguments.
    Your application starts, and outputs to the console - now you want in that very output console interact with the application??
    Or do yo mean you want your application to spawn two consoles, one for input and one for output?

    Not sure what you mean by this...
    Well, how do you suppose you can start an application with arguments with out a console?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #10
    Join Date
    Mar 2010
    Posts
    319
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 14 Times in 12 Posts

    Default Re: Both a command line and GUI application at the same time?

    Quote Originally Posted by high_flyer View Post
    I don't understand how you mean that.
    Lets say you have an open console, and you start you application with arguments.
    Your application starts, and outputs to the console - now you want in that very output console interact with the application??
    Or do yo mean you want your application to spawn two consoles, one for input and one for output?
    As you said, let's say that I have an open console. Now, what I would like is that if I start my application from that console, but with no argument, then I just want the GUI to show up. However, if from that same console window, I start my application with some arguments, then I want my application to take advantage of the console and interact with it (for output only, since I wouldn't expect to need it for input, though one can never tell for certain), and this is where my problem lies... I can't seem to be able to do that in a neat way on Windows while it all works fine on Linux and Mac OS X (even though I do get to see the application icon popping up temporarily on Mac OS X, but I am 'OK' with it I suppose).

    Quote Originally Posted by high_flyer View Post
    Well, how do you suppose you can start an application with arguments with out a console?
    Well, you can always create a shortcut which points to your application and which has arguments associated with it, but this might be besides the point indeed.

  11. #11
    Join Date
    Mar 2010
    Posts
    319
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 14 Times in 12 Posts

    Default Re: Both a command line and GUI application at the same time?

    I had never noticed this: QApplication::QApplication ( int & argc, char ** argv, bool GUIenabled ). Now, if only it was working on all three platforms... Argh!

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Both a command line and GUI application at the same time?

    Now I learned something new too!

    Now, if only it was working on all three platforms... Argh!
    Where does it say that it doesn't?
    It only says that on Windoes and Mac the windowing system is intialized, but that is not a a problem for you.

    Also, I don't see how this actually solvers your problem of spawning a console...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 1
    Last Post: 7th September 2010, 15:49
  2. Replies: 3
    Last Post: 23rd February 2010, 04:33
  3. QProcess and the command line
    By auba in forum Qt Programming
    Replies: 17
    Last Post: 27th May 2009, 10:39
  4. Replies: 0
    Last Post: 28th February 2009, 23:18
  5. Retrieving command line arguments inside a Qt application
    By prykHetQuo in forum Qt Programming
    Replies: 5
    Last Post: 14th February 2009, 14:28

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.