I tried the following code snippet compiled with CONFIG += console set in the .pro file:
#include <QtGui>
#include <QApplication>
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char *argv[])
{
if(argc < 2)
{
cout << "GUI project" << endl;
FreeConsole();
w.show();
}
else
{
cout << "Console project" << endl;
return 0;
}
return a.exec();
}
#include <QtGui>
#include <QApplication>
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
if(argc < 2)
{
cout << "GUI project" << endl;
FreeConsole();
w.show();
}
else
{
cout << "Console project" << endl;
return 0;
}
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Running from command line without arguments just opens the empty QMainWindow. The console where I called the program from shows output "Gui project".
Running from command line with an arbitrary argument just outputs "Console project" to the calling console.
Next use Explorer to start the executable. In this case, a new console and the empty QMainWindow appear. But due to the call to FreeConsole(), the new console gets removed quickly.
So if that "flickering" of the console is ok for you, this might be a solution.
Bookmarks