Open console with GUI only if Qapp is called with true argument.
-
wrote on 10 Aug 2022, 08:22 last edited by
Hello,
I nned help because I want to have my QApplication is opened with a console only when I'm calling it with for example :
C:/app.exe true
The goal is to have only one exe but with argument true, the console can be open to show qdebug messages, and not with arguments false.
I know how to have the console on with includung CONFIG+= console inside pro file, but how to disable it with argument false?
Thank you for your help
Philippe
-
Hello,
I nned help because I want to have my QApplication is opened with a console only when I'm calling it with for example :
C:/app.exe true
The goal is to have only one exe but with argument true, the console can be open to show qdebug messages, and not with arguments false.
I know how to have the console on with includung CONFIG+= console inside pro file, but how to disable it with argument false?
Thank you for your help
Philippe
wrote on 10 Aug 2022, 08:25 last edited by@filipdns
This has been asked before. A Windows executable is either a console application or a UI application: this is determined at link time (and I imagine it's whatCONFIG+= console
affects). You cannot alter this at runtime for the same executable. -
wrote on 10 Aug 2022, 08:54 last edited by
hello,
I found the solution using FreeConsole();
best regards
Philippe
-
wrote on 10 Aug 2022, 09:16 last edited by JonB 8 Oct 2022, 09:19
@filipdns
Then presumably you do not useCONFIG+= console
? Or you do still use that if you use the Windows SDKFreeConsole()
? I can only think you are build a Windows UI app and switching on/off the display of a console, but not just building a console app?Doesn't the console flash up and show, before you hide it? This is discussed in, say, https://stackoverflow.com/questions/66185000/how-can-i-do-the-freeconsole-but-faster-on-c, and elsewhere.
-
wrote on 10 Aug 2022, 09:32 last edited by
In pro file I add CONFIG+= console and when I don't want to have the console appear, then I pass my argument to false, example C:/app.exe false
and inside my app I add:
int main(int argc, char *argv[]) { QApplication a(argc, argv); bool debug=false; debug=QString(argv[1])=="false"?false:true; if(!debug){ FreeConsole(); } ...
-
In pro file I add CONFIG+= console and when I don't want to have the console appear, then I pass my argument to false, example C:/app.exe false
and inside my app I add:
int main(int argc, char *argv[]) { QApplication a(argc, argv); bool debug=false; debug=QString(argv[1])=="false"?false:true; if(!debug){ FreeConsole(); } ...
wrote on 10 Aug 2022, 09:51 last edited by@filipdns
Great, so at least I understand. You have a UI application (QApplication
) and you haveCONFIG+= console
. I didn't know that was allowed. I guess that makes a Windows UI application also create and show a separate window which is a console? Maybe (I don't know) the same could be achieved withoutCONFIG+= console
and instead havingif (debug) AllocConsole()
.BTW, have you ensured your code works both ways if run outside Qt Creator?
-
@filipdns
Great, so at least I understand. You have a UI application (QApplication
) and you haveCONFIG+= console
. I didn't know that was allowed. I guess that makes a Windows UI application also create and show a separate window which is a console? Maybe (I don't know) the same could be achieved withoutCONFIG+= console
and instead havingif (debug) AllocConsole()
.BTW, have you ensured your code works both ways if run outside Qt Creator?
wrote on 11 Aug 2022, 07:43 last edited byI am with @JonB. A console app on Windows cannot have an event loop and widgets. And a GUI application does not show its console by default. @filipdns have you tried to show a window with your current approach? I doubt that it works. From my understanding
CONFIG
needs to be withoutconsole
to have GUI application on Windows (just as @JonB said). Then you need to useAllocConsole()
to create a console instead of usingFreeConsole()
. An additional benefit is that there will be no flickering of the console when it is not enabled. This should be the proper way. -
wrote on 11 Aug 2022, 13:52 last edited by
How does QtCreator's 'Run in terminal' option work to open a Windows console when starting an application within QtCreator?
-
How does QtCreator's 'Run in terminal' option work to open a Windows console when starting an application within QtCreator?
wrote on 11 Aug 2022, 14:10 last edited by@mchinand
Well, the point is that Creator is doing that opening, and attaching the outputs as necessary. It does not change your application to do so. Which is why I suggested the OP test whether what works presently works outside Creator. Creator is doing something likeAllocConsole()
(Windows) to create that terminal and attach it. -
I am with @JonB. A console app on Windows cannot have an event loop and widgets. And a GUI application does not show its console by default. @filipdns have you tried to show a window with your current approach? I doubt that it works. From my understanding
CONFIG
needs to be withoutconsole
to have GUI application on Windows (just as @JonB said). Then you need to useAllocConsole()
to create a console instead of usingFreeConsole()
. An additional benefit is that there will be no flickering of the console when it is not enabled. This should be the proper way.wrote on 17 Aug 2022, 08:04 last edited by@SimonSchroeder Thank you for your suggestion but with -=console in pro file and Alloconsole(), I have terminal openning yes but no qDebug or console.log information display on it.
The +=console method with Freeconsole() work fine for me even I have a very short time a console appearing when I'm starting my application when I don't want a console.
-
@SimonSchroeder Thank you for your suggestion but with -=console in pro file and Alloconsole(), I have terminal openning yes but no qDebug or console.log information display on it.
The +=console method with Freeconsole() work fine for me even I have a very short time a console appearing when I'm starting my application when I don't want a console.
wrote on 17 Aug 2022, 08:10 last edited by JonB@filipdns said in Open console with GUI only if Qapp is called with true argument.:
Alloconsole(), I have terminal openning yes but no qDebug or console.log information display on i
You would presumably have to do stuff to redirect stdout/err etc. to the allocated console.
-
@filipdns said in Open console with GUI only if Qapp is called with true argument.:
Alloconsole(), I have terminal openning yes but no qDebug or console.log information display on i
You would presumably have to do stuff to redirect stdout/err etc. to the allocated console.
wrote on 18 Aug 2022, 06:37 last edited by@JonB said in Open console with GUI only if Qapp is called with true argument.:
You would presumably have to do stuff to redirect stdout/err etc. to the allocated console.
You certainly have to: https://stackoverflow.com/a/57241985/6954968
1/12