Configure QApplication to work like QCoreApplication?
-
Hey
Is there a way to configure QApplication to behave like a command line?
I'm trying to build app that can support both options, but when I run in the command line I can get a crash as some widgets complain that there is no QApplication.
I know its more of an architecture issue that I build - not having enough experience in building command-line apps.
So I was wondering if there are any flags that i can set to have it "hot fixed" without the need of rewriting big chunks of my app.
Right now my gui app in cmd never really "starts", or shows up & hides. I tried to make my own console on windows but that works on 1 pc but breaks on another... sigh.
Regards
Dariusz -
No, this is not possible. But you can instantiate either a QCoreApplication or QApplication in your main.cpp depending on e.g. a command line option.
-
@Christian-Ehrlicher I see, thank you!
One more question then... I'm passing WIN32 in my cmake when building the app. This seems to remove output that would go to console... is there a way to get it back?
On windows I've guild console back using this>
#include "consoleapi.h" #include "consoleapi2.h" #include "WinBase.h" void Console() { AllocConsole(); FILE *pFileCon = NULL; pFileCon = freopen("CONOUT$", "w", stdout); COORD coordInfo; coordInfo.X = 130; coordInfo.Y = 9000; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coordInfo); SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS); }
It runs fine on my pc, but on another pc it shows up & dies instnatly.
-
@Dariusz said in Configure QApplication to work like QCoreApplication?:
One more question then... I'm passing WIN32 in my cmake when building the app. This seems to remove output that would go to console... is there a way to get it back?
Don't pass WIN32 then if you want a console.
-
@Christian-Ehrlicher I want both, An app that can be run from console with arguments and app that can run from exe as gui. But running from the console does not print anything in to console :- (
-
Hi, I had more or less the same problem, solved it by creating a small console (non-gui) Qt app that first uses QSharedMemory to see if the normal gui app is running.
If it's not, then I launch the gui app from the console app, with the arguments the console app got on the command line. -
@Dariusz said in Configure QApplication to work like QCoreApplication?:
I want both, An app that can be run from console with arguments and app that can run from exe as gui. But running from the console does not print anything in to console :- (
Well, I think you can also have a normal graphical app without WIN32 (that is, SUBSYSTEM:console on linker command line). Just that it will always be 'attached'to the console.
If you want to avoid this, you can try a stunt like the installer framework does that 'restarts' itself to detach from the console: https://code.qt.io/cgit/installer-framework/installer-framework.git/tree/src/sdk/main.cpp
-
When I researched having both a GUI and command line application on Windows I stumbled upon how Microsoft does it itself with
devenv
. They provide a.exe
and a.com
at the same time. If you call your softwaremyapp
then you should compile the GUI application tomyapp.exe
and the command line application tomyapp.com
. When typingmyapp
on the command line it will launch the.com
version.What we did in addition is to have the two application call each other depending on the command line parameters.
This is a better user experience than attaching a console.
With
qmake
use an additionalCONFIG
to select between GUI and console (e.g.CONFIG+=console
when calling qmake). In yourmain()
have#ifdef
s to switch between command line and GUI during compilation. -