2

If I open my application an empty console window appears, since I added CONFIG += console to my .pro file. I need the console, because I've implemented a CLI, where some stuff needs to get printed out on the console. On Linux and Mac OSX, I don't actually need the CONFIG += console there. It just works.

How can I prevent opening a windows console, if the .exe gets executed normally over a double click, but display some outputs if my .exe gets started via a console window?

Basically, I use qDebug() << "myText"; and then after that I exit the application with return 0;.

Niklas
  • 23,674
  • 33
  • 131
  • 170

4 Answers4

1

Unfortunately, Windows is somewhat deficient in this area. A console application will always open up a console, even if you don't want it. You can close it right away, but it still looks bad.

Your application must be a non-console application. On startup, check if you have access to a console, as you would when launched from cmd.exe. Then access cmd's console and inject your output into it.

See my question about this for details.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

It is a GUI application? AFAIK it is not possible (or at least not trivial) writing a mixed Qt application that can act as both, a desktop (GUI) application and a console (CLI) application.

I am not sure what you intend to do. If you really need a console variant, try to build two different applications based on the same sources (one console build and one GUI build).

If you only need a GUI application that is able to print out some information, remove the console code and write the output into a file instead.

Silicomancer
  • 8,604
  • 10
  • 63
  • 130
0

I think here is an answer for you question: https://stackoverflow.com/a/3370017/1091536

You need console application, than launch your GUI application and prints it's output.

The application for launch your GUI application you can find here https://github.com/gomons/AppDebugLauncher

Community
  • 1
  • 1
gomons
  • 1,946
  • 14
  • 25
0

It's worth noting that under some circumstances the console window won't briefly appear. For example if you run in gui mode via a shortcut with Run set to Minimized: the console window won't appear. Then, in your code, you can restore the size of the gui window. Its a bit of a nasty workaround but masks the behaviour from the user that bit more.

If you're program is going to be installed and usually started via shortcut, then its maybe its an option.

Fiery
  • 49
  • 5