Turtle10000
Turtle10000

Reputation: 252

How to hide windows Terminal opened in Qt app?

First of all, there are similar questions but I think it's a different question because the Windows terminal appears only when I call system() and not when the app is launching in general.

I have a program with a gui written in C++ with Qt and built in qmake with a .pro file, using MinGW compiler. OS is Windows 10 in a virtual box.

I wrote the code in Ubuntu where it works as intended (no OS terminal visible at all), but now I am testing it in Windows. My Problem is that whenever I call system() or similar in my code to execute another program (supposedly in the background), Windows opens a cmd terminal in front of my gui. I have lots of these calls in my program and therefore there are lots of these windows popping up and disappearing while it runs.

I set main() to WinMain() and removed console from the qmake config, but that did not show any effect.

My .pro file (removed comments):

QT       += core gui

CONFIG -= console
QMAKE_CXXFLAGS += -std=c++11

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = regionfind-gui
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
        main.cpp \
        regionfind.cpp \
        sagacmd.cpp

HEADERS += \
        regionfind.h \
        sagacmd.h \
        processingsteps.h
FORMS += \
        regionfind.ui

RESOURCES += \
        images.qrc

Build command (generated from QtCreator):

C:\Qt\Qt5.12.3\5.12.3\mingw73_64\bin\qmake.exe C:\Users\dev\Documents\regionfind-gui\regionfind-gui.pro -spec win32-g++ && C:/Qt/Qt5.12.3/Tools/mingw730_64/bin/mingw32-make.exe qmake_all

How do I get rid of these terminal windows? They do not open when the command does not have any output, but I can't suppress the general output or similar because in some cases I'm storing the output and printing it into my gui, using the following code:

array<char, 128> buffer;
string result = getDescription(getStep()) + command + "\n";
unique_ptr<FILE, decltype(&pclose) > pipe(popen(command.c_str(), "r"), pclose);
if(!pipe) throw std::runtime_error("popen() failed");
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)  {
 result += checkOutput(buffer.data());
 getGui()->txtOutput->append(QString::fromStdString(removeNewlineAtEnd(buffer.data())));
 getGui()->txtOutput->repaint();
 cout << buffer.data() << flush;;
}

Examples: system("mkdir C:\\Users\dev\\Documents\\testfolder") does not open a window (no output) but system("driverquery") does.

Upvotes: 1

Views: 2390

Answers (1)

Gasteizko
Gasteizko

Reputation: 208

You can do it by using the QProcess class:

    QObject *parent;
    ...
    QString program = "driverquery";

    QProcess *myProcess = new QProcess(parent);
    myProcess->start(program);
    if (myProcess->waitForStarted(-1)) {
        while(myProcess->waitForReadyRead(-1)) {
            getGui()->txtOutput->append( myProcess->readAllStandardOutput() );
       }
    }
    // else report error or whatever

In the case of using a command application, you can do the following:

    QObject *parent;
    ...
    QString program = "driverquery";

    QProcess *myProcess = new QProcess(parent);
    QString command = QString("cmd.exe %1 \"%2 \"").arg(" /C ").arg(program);
    myProcess->start(command);
    if (myProcess->waitForStarted(-1)) {
        while(myProcess->waitForReadyRead(-1)) {
            getGui()->txtOutput->append( myProcess->readAllStandardOutput() );
       }
    }
    // else report error or whatever

Upvotes: 2

Related Questions