vico
vico

Reputation: 18221

Qt widget application output to console

I'm developing a GUI application based on widget in Ubuntu. I would like to print some debug information to console with printf. Is it possible to show console window while debugging application in Qt?

Upvotes: 2

Views: 4974

Answers (2)

C. Haas
C. Haas

Reputation: 29

Yes, that's easily possible. Since you're working with a Qt Application, I would utilize the Qt debugging module; QDebug. To have the console appear on a GUI application you need to edit the CONFIG parameter in the *.pro file like below:

CONFIG += console

This will force any Qt Application to also spawn a console along side it, even if started with a desktop shortcut. Finally, instead of using printf(..), I would use qDebug(). It's built into Qt and simplistic to use. In the files you would like to use qDebug, just add:

#include <QDebug>

When you would like to output a message to the console just write:

qDebug() << "This will output to the spawned console!";

or,

qDebug() << QString("This will output to the spawned console!");

Finally, using the qDebug method allow's you to provide classes with debug operators like below:

class MyClass {
    public:
        MyClass(..);

        QDebug operator<< (QDebug d, const MyClass &myclass) {
            d << "This is what I want to output to the console!";
            return d;
        }

This will allow you to make cleaner code than using fprint(..) throughout your project, hope it helps!

Upvotes: 2

Jesper Juhl
Jesper Juhl

Reputation: 31468

Sure. Just run your application in the debugger from a terminal (like gdb myapp) or instruct your IDE (whatever it is) to run the program in a terminal - both qtcreator, visual studio and others support this.

Upvotes: 2

Related Questions