PDA

View Full Version : Switching between console and GUI on startup



EricF
9th January 2008, 19:25
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 :



#include <QApplication>
#include <QtGui>

// for cout
#include <iostream>
using namespace std;

int main( int argc, char** argv )
{
bool console = false;
if ( argc > 1 )
{
// Check if user selected console mode.
for ( int i=1; i<argc; ++i )
{
if ( !strcmp( argv[i], "-console" ) )
{
console = true;
break;
}
}
}

if ( console )
{
QCoreApplication app( argc, argv );

// This is not working.
cout << "Console Mode" << endl;

// Try to write a file.
QFile myFile( "C:\\Temp\\Test.txt" );
myFile.open( QIODevice::WriteOnly | QIODevice::Text );

QDataStream stream( &myFile );
stream << "Console is working but not showing";

return 0;
}
else
{
QApplication app(argc, argv);

QLabel* label = new QLabel( "GUI !!!" );
label->show();

return app.exec();
}
}


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 ?

wysota
9th January 2008, 20:03
How about:


#include <QApplication>

int main(int argc, char **argv){
bool gui = true;
for(int i=1; i<argc; i++)
if(!strcmp(argv[i], "--console")){
gui = false;
break;
}
QApplication app(argc, argv, gui);
//...
return app.exec();
}

EricF
9th January 2008, 20:14
I feel so dumb now ... Big RTFM for me lol

EricF
10th January 2008, 18:35
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 ...

jacek
10th January 2008, 19:03
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).