2

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?

1
  • If you use Qtcreator this should be standard when you run it in debug mode. Commented Apr 3, 2018 at 13:26

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How to tell QT to run application in terminal?
@vico Qt or no Qt is irrelevant. step 1) open a terminal window. step 2) navigate to the directory with your application. step) run your application from the terminal window.
2

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.