views:

2841

answers:

7

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.

+2  A: 

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
John T
Is that something I can set within Qt Creator?
Paul Dixon
yes, read my edit as well
John T
+1  A: 

Sounds like your linker settings are set for a console app. You could try setting the linker to use the /subsystem:windows option. The option for console applications is /subsystem:console. If you build your project in Visual Studio, create a Win32 Project. Don't create a Win32 Console App. The option under Visual Studio for setting the subsystem is under Project Settings->Linker->System->SubSystem. Select Windows(/subsystem:windows).

I took a look into how to do this with MinGW/gcc. The online docs for gcc shows the different options for Windows targets. Like other have stated, the option you are looking for is -mwindows. The option -mwindows creates a windowed app. It seems like your current setting is -mconsole. The option -mconsole produces a console app. This is a linker option. I'm not familiar with Qt Creator but I'm guessing it has a similar way to view project settings like Visual Studio has.

zooropa
These options are for the Microsoft linker.
zooropa
Yes, I'd like to understand why this occurs using Mingw though.
Paul Dixon
A: 

By default the linker assumes you want a console application.

The fix is to add "-mwindows" to your compiler's argument list, and this will also invisibly add the required libraries (-lkernel32, -lgdi32, etc.). You'll probably need to change from main() to WinMain() as the entry point.

RE: your Edit: the Qt build system uses "spec" files to add variables, you can find them in $QTDIR/qt/mkspecs. When you create a new Windows app in QCreator, it sets the default spec to "win32-g++", which automatically sets the correct variables (windows app, linker flags, etc.).

When you made the project under Linux, it used another spec and that caused your issue. You can see the current spec in the "yourproject.pro.user" file.

Alex Ruse
You don't call the entry point WinMain in a Qt app
Paul Dixon
OK, 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.
Alex Ruse
A: 

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.

bluebrother
+5  A: 

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.

Paul Dixon
A: 

Qt/2009.05/qt/mkspecs/win32-g++
edit file qmake.conf
modify line
QMAKE_LFLAGS_CONSOLE = -Wl,-subsyetem,console
to
QMAKE_LFLAGS_CONSOLE = -Wl,-subsyetem,windows

and modify your ???.pro file (example add new line or space)
and compiling
it`s working ok

Eda
A: 

Hallo,

I think yo are correct. Including testlib, makes the console window appear and removing it supperesses it.

But my application still demands testlib to be present (because i badly wanted to use QTest::qWait() method), and no console window to appear. I tried a few options from this page but nothing is working. what should i do?

The options i tried are

  1. adding CONFIG -= console to my .pro file.

        QT += gui \    
            xml \
            core \
            testlib    
        CONFIG += qt \
            windows \
            uitools \
            release        
        CONFIG -= console        
        LIBS += extern/ftd2xx.lib        
        RC_FILE = res/AC-Core.rc        
        QMAKE_CXXFLAGS += -Wall \
    
  2. editing the qmake.conf file, the lines

    QMAKE_LFLAGS_CONSOLE = -Wl,-subsystem,console, to

    QMAKE_LFLAGS_CONSOLE = -Wl,-subsystem,windows

Any other ideas to suppress the appearing of console window?

Thanks!

Hari Kishan