Lincoln Ingaroca
Lincoln Ingaroca

Reputation: 116

CMake shows console

I made an app and when I run in QtCreator everything goes well, my question is when I generate the release, a consola is shown along with my app, why does this happen, and how can I avoid it.

Here my code.

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlTableModel>


MainWindow::MainWindow(QWidget *parent)
  : QMainWindow(parent), ui(new Ui::MainWindow)
{
  ui->setupUi(this);
  QSqlDatabase db=QSqlDatabase::addDatabase("QPSQL");
  if(!db.isDriverAvailable("QPSQL")){
      QMessageBox::critical(this,qApp->applicationName(),db.lastError().text());
      return;
    }
  db.setDatabaseName("covid");
  db.setHostName("localhost");
  db.setPassword("2311046");
  db.setPort(5432);
  db.setUserName("postgres");
  if(!db.open()){
      QMessageBox::critical(this,qApp->applicationName(),db.lastError().text());
      return;
    }
  QMessageBox::information(this,qApp->applicationName(),"Todo bien");
}

MainWindow::~MainWindow()
{
  delete ui;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

project(untitled1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.

#if(ANDROID)
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
#    if (ANDROID_ABI STREQUAL "armeabi-v7a")
#        set(ANDROID_EXTRA_LIBS
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
#    endif()
#endif()

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
find_package(Qt5 COMPONENTS Sql REQUIRED)

if(ANDROID)
  add_library(untitled1 SHARED
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
  )
else()
  add_executable(untitled1
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
  )
endif()

target_link_libraries(untitled1 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(untitled1 PUBLIC Qt${QT_VERSION_MAJOR}::Sql)

Here I show the Windows that are shown every time I run the .exe:

enter image description here

Upvotes: 1

Views: 922

Answers (1)

Alex Reinking
Alex Reinking

Reputation: 19926

This should be quite simple. Just add:

# ...
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Make sure we build executables as GUI apps, rather than console apps, on Windows
set(CMAKE_WIN32_EXECUTABLE TRUE)
# ...

As the documentation explains:

When [WIN32_EXECUTABLE] is set to true the executable when linked on Windows will be created with a WinMain() entry point instead of just main(). This makes it a GUI executable instead of a console application. [...] This property is initialized by the value of the CMAKE_WIN32_EXECUTABLE variable if it is set when a target is created.

Upvotes: 1

Related Questions