Open console with GUI only if Qapp is called with true argument.
-
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
-
@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.
-
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(); } ...
@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?
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. -
How does QtCreator's 'Run in terminal' option work to open a Windows console when starting an application within QtCreator?
@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.@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.
@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.
@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