Results 1 to 12 of 12

Thread: how to enable an app to be executed in GUI mode and console mode

  1. #1
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default how to enable an app to be executed in GUI mode and console mode

    Hi guys,

    I made an App with GUI. But now I would like to manage the options in the command line.

    Examaple, if I execute the MyApp.exe without parameters (or by clicking the icon), then It should launch the Main window, etc.

    But If I execute form the command line something like MyApp.exe -version, I would like to get only the version, without launching the App.

    I modified the .pro file and added
    Qt Code:
    1. CONFIG += console
    To copy to clipboard, switch view to plain text mode 

    in order to return the version with a cout sentence in the main.cpp.

    But now if I execute the app without any argument, the console is launched at the same time of the main window. So the console keeps behind the main window.

    What am I missing? There is another way to do that?

    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: how to enable an app to be executed in GUI mode and console mode

    This should be very easy:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. if (argv[1] == "-version") {
    4. printf("Version");
    5. exit 0;
    6. }
    7.  
    8. QApplication a(argc, argv);
    9.  
    10. ...
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to enable an app to be executed in GUI mode and console mode

    This is not the problem. I've made alreadyt the code, the problem is that, If I configure the .pro file as CONFIG += console. Then when I start the app with no arguments, it lauches the console and the main window as well. I this case I dont want to see thjie console, just the main window.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: how to enable an app to be executed in GUI mode and console mode

    Don't add config += console

  5. #5
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to enable an app to be executed in GUI mode and console mode

    If I dont add config+=console, when I rund the program from the command promp with the -version argument, then nothing appears.

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: how to enable an app to be executed in GUI mode and console mode

    Then I don't know. Is this even possible in Windows?

    Here you have a GUI application where you can perfectly add an about dialog with the version number.
    Why do you want to print that in the console?

  7. #7
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to enable an app to be executed in GUI mode and console mode

    I don't think that the behavior you want is possible under Windows platform...

  8. #8
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to enable an app to be executed in GUI mode and console mode

    I want to print it in the console because an external software will made some checks about the version. So the real user will run the app just from the icon (without parameters, ) but the other checking software will run it with the versiona argument, in order to get the version and make some checks.

  9. #9
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to enable an app to be executed in GUI mode and console mode

    Nice but not possible on Windows. Though you can always try with IPC, if you have the sources of the other software.

  10. The following user says thank you to The Storm for this useful post:

    locke (19th August 2010)

  11. #10
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: how to enable an app to be executed in GUI mode and console mode

    Isn't it possible to set a version number for an exe just like you do for a DLL?

  12. #11
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to enable an app to be executed in GUI mode and console mode

    That sounds a bit complicate. Thanks anyways

  13. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to enable an app to be executed in GUI mode and console mode

    You can use AllocConsole(). If your app already has a console it'll fail, otherwise it'll open a new console and set the appropriate handles for you (by default, GUI applications do not have a console)

    You can then use GetStdHandle(STD_OUTPUT_HANDLE) to get a handle to your console to which you can write to using WriteFile() or WriteConsole().

    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    http://msdn.microsoft.com/en-us/libr...=VS.85%29.aspx

  14. The following user says thank you to squidge for this useful post:

    locke (19th August 2010)

Similar Threads

  1. Replies: 1
    Last Post: 2nd November 2009, 13:02
  2. How to create single exe for Console & GUI mode
    By summer_of_69 in forum Qt Programming
    Replies: 4
    Last Post: 11th June 2009, 08:20
  3. Replies: 9
    Last Post: 15th April 2009, 07:23
  4. Replies: 8
    Last Post: 10th October 2007, 19:20
  5. Console in GUI Mode
    By plitex in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 21:12

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.