Results 1 to 5 of 5

Thread: Switching between console and GUI on startup

  1. #1
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Switching between console and GUI on startup

    I'm trying to add console support to my already working application. So I get in the main to add some switch support to the command line but it is not poping any command line when entering the right switch. Here's a little main In crafted to try this coexistence :

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. // for cout
    5. #include <iostream>
    6. using namespace std;
    7.  
    8. int main( int argc, char** argv )
    9. {
    10. bool console = false;
    11. if ( argc > 1 )
    12. {
    13. // Check if user selected console mode.
    14. for ( int i=1; i<argc; ++i )
    15. {
    16. if ( !strcmp( argv[i], "-console" ) )
    17. {
    18. console = true;
    19. break;
    20. }
    21. }
    22. }
    23.  
    24. if ( console )
    25. {
    26. QCoreApplication app( argc, argv );
    27.  
    28. // This is not working.
    29. cout << "Console Mode" << endl;
    30.  
    31. // Try to write a file.
    32. QFile myFile( "C:\\Temp\\Test.txt" );
    33. myFile.open( QIODevice::WriteOnly | QIODevice::Text );
    34.  
    35. QDataStream stream( &myFile );
    36. stream << "Console is working but not showing";
    37.  
    38. return 0;
    39. }
    40. else
    41. {
    42. QApplication app(argc, argv);
    43.  
    44. QLabel* label = new QLabel( "GUI !!!" );
    45. label->show();
    46.  
    47. return app.exec();
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 

    The file gets written but I don't get any output to the console. I added the switch to tell it is a command line app and I can see the output however, I will get a console even if I'm in GUI mode.

    How can I achieve an application that can be executed in either mode ?

    Another question, does my -console switch is already used by Qt ?
    Attached Files Attached Files

  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: Switching between console and GUI on startup

    How about:

    Qt Code:
    1. #include <QApplication>
    2.  
    3. int main(int argc, char **argv){
    4. bool gui = true;
    5. for(int i=1; i<argc; i++)
    6. if(!strcmp(argv[i], "--console")){
    7. gui = false;
    8. break;
    9. }
    10. QApplication app(argc, argv, gui);
    11. //...
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to wysota for this useful post:

    EricF (9th January 2008), pherthyl (9th January 2008)

  4. #3
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Switching between console and GUI on startup

    I feel so dumb now ... Big RTFM for me lol

  5. #4
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Switching between console and GUI on startup

    I may have felt dumb but it's still not working as expected, application don't output to console but is writing the file. So my question remains ...

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Switching between console and GUI on startup

    IMO the problem might be in windows' subsystems. You are using win32 subsystem now, but console applications use different one. You should be able to find some info about this in MSDN (most likely it's a matter of setting right linker flags).

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.