Console output in Qt GUI application?

I have a Qt GUI application running on Windows that allows me to pass command line parameters, and in some cases I want to display a message on the console and then exit, for example:

int main(int argc, char *argv[]) { QApplication a(argc, argv); if (someCommandLineParam) { std::cout << "Hello, world!"; return 0; } MainWindow w; w.show(); return a.exec(); } 

However, console messages do not appear when I launch the application from the command line. Does anyone know how I can make this work?

+41
c ++ windows qt qt4
Jul 29 '10 at 8:14
source share
15 answers

Windows really does not support dual-mode applications.

To see the console output, you need to create a console application

 CONFIG += console 

However, if you double-click on the program to launch the GUI mode version, you will get a console window, which is probably not what you want. To prevent the console window from appearing, you need to create a GUI mode application, in which case you will not get an output in the console.

One idea might be to create a second small application, which is a console application and provides output. This may cause a second job.

Or you could put all the functions in a DLL, and then create two versions of the .exe file that have very simple core functions that are called in the DLL. One for the GUI and one for the console.

+39
Jul 30 '10 at 8:59
source share

There is no way to output a message to the console when using QT += gui .

fprintf(stderr, ...) also cannot print output.

Use the QMessageBox instead to display the message.

+6
May 29 '11 at 17:05
source share

Add

 #ifdef _WIN32 if (AttachConsole(ATTACH_PARENT_PROCESS)) { freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr); } #endif 

at the top of main() . This will allow output to the console only if the program is running in the console and will not open the console window in other situations. If you want to create a console window for displaying messages when starting the application outside the console, you can change the condition:

 if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole()) 
+5
Jan 17 '17 at 15:35
source share

Oh, you can display a message when using QT += gui and CONFIG += console .

You need printf("foo bar") , but cout << "foo bar" does not work

+4
Jan 04 '12 at 19:13
source share
 void Console() { AllocConsole(); FILE *pFileCon = NULL; pFileCon = freopen("CONOUT$", "w", stdout); COORD coordInfo; coordInfo.X = 130; coordInfo.Y = 9000; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coordInfo); SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE),ENABLE_QUICK_EDIT_MODE| ENABLE_EXTENDED_FLAGS); } int main(int argc, char *argv[]) { Console(); std::cout<<"start@@"; qDebug()<<"start!"; 

You cannot use std :: cout, as others have said, my method is perfect even for some code that cannot include "qdebug"!

+4
Dec 22 '13 at 19:56
source share

I used this header below for my projects. Hope this helps.

 #ifndef __DEBUG__H #define __DEBUG__H #include <QtGui> static void myMessageOutput(bool debug, QtMsgType type, const QString & msg) { if (!debug) return; QDateTime dateTime = QDateTime::currentDateTime(); QString dateString = dateTime.toString("yyyy.MM.dd hh:mm:ss:zzz"); switch (type) { case QtDebugMsg: fprintf(stderr, "Debug: %s\n", msg.toAscii().data()); break; case QtWarningMsg: fprintf(stderr, "Warning: %s\n", msg.toAscii().data()); break; case QtCriticalMsg: fprintf(stderr, "Critical: %s\n", msg.toAscii().data()); break; case QtFatalMsg: fprintf(stderr, "Fatal: %s\n", msg.toAscii().data()); abort(); } } #endif 

PS: you can add dateString for output if you want in the future.

+2
Jul 29 '10 at 8:32
source share

Something you can explore, at least for windows, is the AllocConsole () function in the windows api. It calls GetStdHandle several times to redirect stdout, stderr, etc. (A quick test shows that this does not completely do what we want it to do. You get a console window open with your other Qt stuff, but you cannot output to it. Presumably, since the console window is open, there is a way get access to it, get a handle to it, or access it and process it somehow.Here is the MSDN documentation for those who are interested in this:

AllocConsole (): http://msdn.microsoft.com/en-us/library/windows/desktop/ms681944%28v=vs.85%29.aspx

GetStdHandle (...): http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx

(I would add this as a comment, but the rules do not allow me to do this ...)

+2
Sep 12
source share

First of all, why do you need to output to the console in the release mode assembly? No one will think to look there when there is a gui ...

Secondly, qDebug is a fantasy :)

Thirdly, you can try adding console to .pro CONFIG , this might work.

0
Jul 29 '10 at 9:08
source share

In your .pro add

 CONFIG += console 
0
Jul 29 '10 at 15:48
source share

Maybe it was control over other answers, or maybe it was a requirement that the user really needed a console output, but the obvious answer for me is to create a secondary window that can be shown or hidden (using a checkbox or button) that displays all messages by adding lines of text to a text window widget and using it as a console?

The advantages of this solution are as follows:

  • A simple solution (assuming all this displays a simple log).
  • The ability to connect the console widget to the main application window. (In Qt, anyway).
  • Ability to create multiple consoles (if more than 1 thread, etc.).
  • It's pretty easy to switch from local console output to sending logs over the network to the client.

I hope this gives you food for thought, although I can in no way qualify the postulate of how you should do this, I can imagine that one of us can do very little with searching / reading!

0
Aug 08 '14 at 1:51 on
source share

Verify that Qt5Core.dll is in the same directory as the executable software application.

I had a similar problem in Qt5 with a console application: if I run the application from Qt Creator, the output text will be visible, if I open cmd.exe and run the same application there, the output is not displayed. Very strange!

I solved this by copying Qt5Core.dll to the directory with the executable application.

Here is my tiny console application:

 #include <QCoreApplication> #include <QDebug> int main(int argc, char *argv[]) { int x=343; QString str("Hello World"); qDebug()<< str << x<<"lalalaa"; QTextStream out(stdout); out << "aldfjals alsdfajs..."; } 
0
Aug 20 '15 at 14:19
source share

I also played with this, finding that the redirect result worked, but I never saw the output in the console window that is present for every Windows application. This is my solution so far until I find a Qt replacement for ShowWindow and GetConsoleWindow.

Run it from the command line without parameters - get a window. Run from the command line with parameters (for example, cmd aaa bbb ccc) - you will get the text in the command prompt window - just as you would expect from any console Windows application.

Please excuse the lame example - it is about 30 minutes to mess around.

 #include "mainwindow.h" #include <QTextStream> #include <QCoreApplication> #include <QApplication> #include <QWidget> #include <windows.h> QT_USE_NAMESPACE int main(int argc, char *argv[]) { if (argc > 1) { // User has specified command-line arguments QCoreApplication a(argc, argv); QTextStream out(stdout); int i; ShowWindow (GetConsoleWindow(),SW_NORMAL); for (i=1; i<argc; i++) out << i << ':' << argv [i] << endl; out << endl << "Hello, World" << endl; out << "Application Directory Path:" << a.applicationDirPath() << endl; out << "Application File Path:" << a.applicationFilePath() << endl; MessageBox (0,(LPCWSTR)"Continue?",(LPCWSTR)"Silly Question",MB_YESNO); return 0; } else { QApplication a(argc, argv); MainWindow w; w.setWindowTitle("Simple example"); w.show(); return a.exec(); } } 
0
Jan 07 '16 at 16:52
source share

After quite a long struggle with the exact same problem, I found that it’s just

 CONFIG += console 

really does the trick. It will not work unless you explicitly tell QtCreator that qmake is running in the project (right-click on the project) and change something inside the source file and then rebuild. Otherwise, the compilation will be skipped, and you still will not see the output on the command line. Now my program works both in GUI mode and in cmd mode.

-one
Jul 21 '16 at 7:06
source share

Easily

Step1: Create a new project. Go File-> New File or Project β†’ Another Project β†’ Empty Project

Step 2: Use the code below.

In the .pro file

 QT +=widgets CONFIG += console TARGET = minimal SOURCES += \ main.cpp 

Step 3: Create the main.cpp file and copy the code below.

 #include <QApplication> #include <QtCore> using namespace std; QTextStream in(stdin); QTextStream out(stdout); int main(int argc, char *argv[]){ QApplication app(argc,argv); qDebug() << "Please enter some text over here: " << endl; out.flush(); QString input; input = in.readLine(); out << "The input is " << input << endl; return app.exec(); } 

I created the necessary objects in the code for your understanding.

Just run it

If you want your program to receive multiple inputs with certain conditions. Then skip the code below in Main.cpp

 #include <QApplication> #include <QtCore> using namespace std; QTextStream in(stdin); QTextStream out(stdout); int main(int argc, char *argv[]){ QApplication app(argc,argv); qDebug() << "Please enter some text over here: " << endl; out.flush(); QString input; do{ input = in.readLine(); if(input.size()==6){ out << "The input is " << input << endl; } else { qDebug("Not the exact input man"); } }while(!input.size()==0); qDebug(" WE ARE AT THE END"); // endif return app.exec(); } // end main 

I hope he educates you.

Good afternoon,

-one
Mar 03 '17 at 14:23
source share

First of all, you can try flushing the buffer

 std::cout << "Hello, world!"<<std::endl; 

For more Qt-based entries, you can try using qDebug.

-2
Jul 29 '10 at 8:18
source share



All Articles