Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Close console in Hybrid application

Close console in Hybrid application

Scheduled Pinned Locked Moved General and Desktop
hybrid console
6 Posts 3 Posters 2.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Saulo Fonseca
    wrote on last edited by Saulo Fonseca
    #1

    Hi everyone,

    I am developing a hybrid GUI/console application. If I give no arguments, it should opens the GUI version. Else, it should runs the console version. Everything works fine, but the GUI version also opens a console window. I want to close this console window so the GUI opens alone. Here is my .pro file:

    QT       += core gui
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    TARGET = Test
    TEMPLATE = app
    CONFIG  += console  # Allows console activity
    SOURCES += main.cpp\
    	mainwindow.cpp
    HEADERS  += mainwindow.h
    FORMS    += \
    	mainwindow.ui
    

    And here is my main:

    #include <QApplication>
    #include <iostream>
    #include "mainwindow.h"
    using namespace std;
    int main(int argc, char *argv[])
    {
    	// Start in GUI mode
    	if (argc == 1)
    	{
    		QApplication a(argc, argv);
    		MainWindow w;
    		w.show(); // Here it shows both a console and GUI windows
                      // I want here only a GUI window
    		return a.exec();
    	}
    	// Start in console mode
    	else
    	{
    		QApplication a(argc, argv);
    		MainWindow w; // Without show() there is only a console window
    		cout << "Console" << endl;
    		return a.exec();
    	}
    }
    

    Is there a way to close this extra console window?

    Thanks in advance.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Saulo Fonseca
      wrote on last edited by
      #2

      I discovered that I can add use freeConsole() like this:

      #include <QApplication>
      #include <iostream>
      #include <windows.h>  // freeConsole()
      #include "mainwindow.h"
      using namespace std;
      int main(int argc, char *argv[])
      {
      	// Start in GUI mode
      	if (argc == 1)
      	{
      		FreeConsole();  // Closes the console window,
                              // but it still shows for a small instant.
      		QApplication a(argc, argv);
      		MainWindow w;
      		w.show();
      		return a.exec();
      	}
      	// Start in console mode
      	else
      	{
      		QApplication a(argc, argv);
      		MainWindow w;
      		cout << "Console" << endl;
      		return a.exec();
      	}
      }
      

      This solution works only on Windows and it's not perfect. It still shows the console window for a small instant. Does anyone has a better idea?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        It's because you added QT += console in your pro file. However, if you don't want any GUI, why not use a QCoreApplication ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Saulo Fonseca
          wrote on last edited by Saulo Fonseca
          #4

          Hi SGaist, thank you for your attention. You misunderstood my question. I want to have both GUI and console. If I provide no command line arguments, it will launch a GUI. If I provide a command line argument, it will run in console. That is my go.

          1 Reply Last reply
          0
          • JohanSoloJ Offline
            JohanSoloJ Offline
            JohanSolo
            wrote on last edited by
            #5

            Why not creating two separate programs and use a launcher script which takes care of launching the one you need?

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Saulo Fonseca
              wrote on last edited by
              #6

              This would be my second option. I like the idea to have everything in one file. If this is possible, I want to use this option.

              1 Reply Last reply
              0
              • Pl45m4P Pl45m4 referenced this topic on

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved