How to use printf in GUI program?

As an old programmer, when I debug a program, the first option is neither using qDebug()<<“helloworld”, nor using cout<<“helloworld”, not mention QMessageBox::information(this,”hello”,”world”). I’d like to use the old printf function.

But how to use the printf function in a GUI program? For example, you may want to use printf in a slot function in a Qt Widget project. If you just write printf(“hello world”) in a slot function, you will see nothing in the Application Output window in Qt Creator until the debugged program ends. This is because the output of printf is buffered. To make the output of printf display in the Application Output window immediately, you should use:

printf("hello world");
fflush(stdout);

So, as a Qt programmer, you’d better use the Qt statement qDebug()<<“helloworld” which outputs immediately without buffering.

The c++ statement cout<<“helloworld” has the same problem as printf, i.e., the output is buffered and you may only see the output at the end of the debugged program. Furthermore, to use cout, you need to add two lines in your source code:

using namespace std;
#include<iostream>

....
....
cout<<"hello world";
fflush(stdout);

 

 

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Leave a Reply