Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep my Qt C++ program from opening a console in Windows?

I'm making an application in Qt Creator, with cmake and MinGW as compiler. I've seen this question being answered for other people, but they used regular Qt projects with .pro files, while I use a CMakeLists.txt file. So these posts were of no help to me.

The problem is that my application opens a console when booted, and as usual, closing this console will close the application as well. I want to keep the application from opening a console, so that it is more user-friendly for people who don't need any debug information and such.

like image 532
Neko Avatar asked Nov 23 '11 21:11

Neko


People also ask

How do I exit Qt console application?

Try to use that code in your MyConsole class: #include <QApplication> ... qApp->quit();

What is Qt console application?

Overview. The Qt console is a very lightweight application that largely feels like a terminal, but provides a number of enhancements only possible in a GUI, such as inline figures, proper multi-line editing with syntax highlighting, graphical calltips, and much more. The Qt console can use any Jupyter kernel. 


1 Answers

By default, and in contrast to qmake, cmake builds Qt apps with enabled console window under windows (windows binaries can use different entry points - the console window is one of them).

You can disable the console window appearing via setting the WIN32_EXECUTABLE cmake property on the executable.

This can be achieved either via setting an add_executable option, i.e.

add_executable(myexe WIN32 ...)

or via setting the property explicitly:

set_property(TARGET main PROPERTY WIN32_EXECUTABLE true)

Using set_property() is helpful when the console window should conditionally be disabled, e.g.:

if(CMAKE_BUILD_TYPE STREQUAL "Release")
  set_property(TARGET main PROPERTY WIN32_EXECUTABLE true)
endif()

The WIN32_EXECUTABLE property has no effect when compiling on platforms other than windows (cf. CMAKE_WIN32_EXECUTABLE).

As with the WIN32 cmake variable, the WIN32_EXECUTABLE property also configures the console window when compiling a win64 executable.

like image 74
maxschlepzig Avatar answered Sep 23 '22 18:09

maxschlepzig