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 )
{
// This is not working.
cout << "Console Mode" << endl;
// Try to write a file.
QFile myFile
( "C:\\Temp\\Test.txt" );
stream << "Console is working but not showing";
return 0;
}
else
{
label->show();
return app.exec();
}
}
#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();
}
}
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 ?
Bookmarks