How to Print to Console When Using Qt

How to print to console when using Qt

If it is good enough to print to stderr, you can use the following streams originally intended for debugging:

#include<QDebug>

//qInfo is qt5.5+ only.
qInfo() << "C++ Style Info Message";
qInfo( "C Style Info Message" );

qDebug() << "C++ Style Debug Message";
qDebug( "C Style Debug Message" );

qWarning() << "C++ Style Warning Message";
qWarning( "C Style Warning Message" );

qCritical() << "C++ Style Critical Error Message";
qCritical( "C Style Critical Error Message" );

// qFatal does not have a C++ style method.
qFatal( "C Style Fatal Error Message" );

Though as pointed out in the comments, bear in mind qDebug messages are removed if QT_NO_DEBUG_OUTPUT is defined

If you need stdout you could try something like this (as Kyle Strand has pointed out):

QTextStream& qStdOut()
{
static QTextStream ts( stdout );
return ts;
}

You could then call as follows:

qStdOut() << "std out!";

Console output in a Qt GUI app?

Windows does not really support dual mode applications.

To see console output you need to create a console application

CONFIG += console

However, if you double click on the program to start the GUI mode version then you will get a console window appearing, which is probably not what you want. To prevent the console window appearing you have to create a GUI mode application in which case you get no output in the console.

One idea may be to create a second small application which is a console application and provides the output. This can call the second one to do the work.

Or you could put all the functionality in a DLL then create two versions of the .exe file which have very simple main functions which call into the DLL. One is for the GUI and one is for the console.

How to print version of a Qt GUI application to console

As we cleared some points in comments let's move on. ;)

Take a look at the documentation (http://doc.qt.io/qt-5/qapplication.html#details). In the detail section you see a sane way how to properly parse and handle command line options.

And here (https://stackoverflow.com/a/3886128/6385043) you can see a possibility for writing to standard output. Notice the QDebug caveat.

In my opinion, stick to the text file. You may generate it during build with qmake using the variable VERSION, which you can also use with QApplication::setApplicationVersion(QString).

qt printing to terminal

To write to stdout, you should add this CONFIG += console to your project file config and use cout of printf for your liking. qDebug prints by default to stderr. Check this topic for more info - How to print to console when using Qt

QJSEngine: print to console

The print function is not implemented in QJSEngine. You will have to implement it yourself. Luckily you can write QObjects and make them available in your script. (See section "QObject Integration" at http://doc.qt.io/qt-5/qjsengine.html)

Here's how i've done it:

Create a class JSConsole inheriting from QObject:

Your own console class

jsconsole.h

#ifndef JSCONSOLE_H
#define JSCONSOLE_H

#include <QObject>

class JSConsole : public QObject
{
Q_OBJECT
public:
explicit JSConsole(QObject *parent = 0);

signals:

public slots:
void log(QString msg);
};

#endif // JSCONSOLE_H

jsconsole.cpp

#include "jsconsole.h"
#include <QDebug>

JSConsole::JSConsole(QObject *parent) :
QObject(parent)
{
}

void JSConsole::log(QString msg)
{
qDebug() << "jsConsole: "<< msg;
}

Using it

Now you create a proxy object in the js engine by using QJSEngine.newQObject.
After that you add it to your global object and use it.

QJSEngine engine;
JSConsole console;
QJSValue consoleObj = engine.newQObject(&console);
engine.globalObject().setProperty("console", consoleObj);
QJSValue result = engine.evaluate("console.log('test');");

Error logging

I've searched a long time for errors in my c++ code, when i just made a spelling error in my js file. The following snippet would have helped avoid that.

if (result.isError())
{
qDebug() << "result: " << result.property("lineNumber").toInt() << ":" << result.toString();
}

PS: First post after years of lurking. I've read tips on writing great answers but if I've made some mistakes/bad things please tell me.

May the code be with you

Qt Creator no longer displays console output in Application Output window after Windows 10 1809 update

For those of you that are interested, I worked around the problem by conditionally calling FreeConsole() at the beginning of the program, like this:

#ifdef Q_OS_WIN
#include <Windows.h>
#endif

...

#ifdef Q_OS_WIN
#if QT_VERSION <= QT_VERSION_CHECK(5, 2, 1)
// Since the Windows 10 1809 update was pushed, the debug process has a
// hidden console window allocated to it. This prevents the application output
// reaching the console window in Qt 5.2.1 (see qlogging.cpp in the qt source for
// reasons why). This detaches that console window, meaning the output is shown in the
// application output window again. However, if "Run in terminal" is selected in the
// Run options then the output will still be shown in the Application Output window.
FreeConsole();
#endif
#endif

print/display something in a Qt function call by a slot

Why you don't use QDebug if you only want to check if it's working? You can put in on_pushButton:

void MainWindow::on_pushButton_clicked()
{
qDebug () << name;
}

The output will be printed to the console of your QT creator (also in GUI mode).



Related Topics



Leave a reply



Submit