Why does my Qt 4.5 app open a console window under Windows?
Solution 1
The short answer is that including the Qt testlib causes the console to appear. Removing it makes it go away.
To explain further, if your .pro file adds testlib to QT, e.g.
QT += sql \
webkit \
network \
testlib
then the final link step is carried out with a line like this
g++ -enable-stdcall-fixup
-Wl,-enable-auto-import
-Wl,-enable-runtime-pseudo-reloc
-mthreads
-Wl
-Wl,-subsystem,console
-o debug\MyApp.exe object_script.MyApp.Debug
-L"c:\Qt\2009.01\qt\lib"
-lglu32 -lgdi32 -luser32 -lQtWebKitd4 -lQtTestd4
-lQtSqld4 -lQtGuid4 -lQtNetworkd4 -lQtCored
We've wound up using the console subsystem! I presume using testlib forces this to happen so that the test output has somewhere to go.
If we now edit the .pro file and remove the reference to testlib and rebuild, we get a link step like this
g++ -enable-stdcall-fixup
-Wl,-enable-auto-import
-Wl,-enable-runtime-pseudo-reloc
-mthreads
-Wl
-Wl,-subsystem,windows
-o debug\Ammotin.exe object_script.Ammotin.Debug
-L"c:\Qt\2009.01\qt\lib"
-lglu32 -lgdi32 -luser32 -lmingw32 -lqtmaind -lQtWebKitd4
-lQtSqld4 -lQtGuid4 -lQtNetworkd4 -lQtCored4
Yay! subsystem is windows, no more console window.
Solution 2
For those who have this issue using CMake under Windows (see Amoo's comment), here's a solution here.
In short, you need to add WIN32
to your add_executable()
statements:
add_executable(GuiApplication WIN32 src/main.cpp)
For further details, see the CMake documentation on add_executable
and WIN32_EXECUTABLE
.
Solution 3
I think that this is not a solution for this specific answer (besides it is 4 years later), but I think that many people landing in this thread will be looking for this setting:
Projects > Build and Run > Run Settings > Run > [x] Run in terminal
De-select it and run your GUI from QtCreator without an extra Terminal window. Terminal output will be then embedded in the IDE.
Solution 4
You will want to make sure the -mwindows switch is provided.
Edit:
alternatively, you can go into your makefile and add this to your linker flags:
-Wl,-subsystem,windows
Solution 5
Make sure your .pro file doesn't add console
to the variable CONFIG
. You can do this by adding
CONFIG -= console
somewhere at the end of your .pro file. If CONFIG
contains console
a window pops up every time you start the program, and this is also used when printing debug output. Thus, adding console
can be useful when debugging a program. Otherwise you'd need to use tools like DebugView to show the output of the qDebug()
calls.
kliron
My mini-bio is here, but I've been professional software developer since 1992. I live in the UK, and I am currently the CTO of LibLynx LLC Some of my popular spare time projects have included Geograph British Isles, pastebin.com. I am mainly active in php, javascript, mysql and c++ questions #SOreadytohelp
Updated on June 18, 2020Comments
-
kliron almost 4 years
I've been playing around with Qt Creator 4.5 under Linux. My application builds just fine under Linux, but if I build in Windows, the app always opens a console window at startup.
Can I stop it doing that?
I'm building using the default MinGW setup, perhaps that is related. If need be I can build with Visual Studio, but I'd like to understand what is happening first...
Edit: I just created a simple test GUI app with Qt Creator under Windows and it didn't exhibit this behaviour. Either this behaviour has occurred because of the way the project was created under linux, or there is something my app does which causes the console window to appear. Will post details when I diagnose it in case it helps someone else.
-
zooropa about 15 yearsThese options are for the Microsoft linker.
-
kliron about 15 yearsIs that something I can set within Qt Creator?
-
kliron about 15 yearsYes, I'd like to understand why this occurs using Mingw though.
-
Alex Ruse about 15 yearsOK, I'm not all that familiar with Qt. A WinMain is required for GUI apps in Windows, so the framework probably implements WinMain() itself and calls your main() behind the scenes. The rest of the post stands, though.
-
David Doria over 11 yearsThis works for a gcc toolchain, but what about with using the MSVC compiler? (it says "unknown option -mwindows").
-
Angie Quijano over 8 yearsHello, I don't have
testlib
in my .pro, and I have the same problem -
Joel Bodenmann about 8 yearsThank you, this is what fixed it for me.
-
fbeeper about 8 yearsGlad it helped somebody :)
-
Grant Rostig over 7 yearsjust what I needed. Thanks.
-
CroCo about 7 yearsThis flag worked in Qt 5.7
QT.testlib.CONFIG -= console
But thisCONFIG -= console
doesn't work. -
Ignas over 4 yearsWorked with CMake on Windows
-
Amoo Hesam almost 4 yearsIf you are Windows and using CMake and the application is showing after terminal when you click it outside of IDE, this is the correct solution for that.
-
Lion almost 4 yearsThanks @AmooHesam for pointing that out. I updated my answer.