Close console in Hybrid application
-
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.
-
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?
-
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 ? -
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.
-
Why not creating two separate programs and use a launcher script which takes care of launching the one you need?
-
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.
-
P Pl45m4 referenced this topic on 1 Jun 2024, 15:57
1/6