How to print to console when using Qt
https://www.baidu.com/link?url=A-5N9ehSXHYCgpr6SzL5ohv2xEsjs-HIZqxIj9_CjFAKwiJgNrIhto9sEu6kXw51-FGFvSglXIJzihkOzuU8Gjucb9KufbAwZJ0uRVhzWFiP7sd6N09FtLkRFoDnL15X&wd=&eqid=b5904d5e0003bbc3000000025aceb0aa
down vote
I found this most useful:
#include <QTextStream>
QTextStream out(stdout);
foreach(QString x, strings)
out << x << endl;
-------------------------------------------------------------
If you incorporate the information from Goz's answer about how to print errors/warnings, along with a bit of information (sadly lacking from Goz's answer but present in the comments below it) about what qDebug() etc actually do, this will be by far the superior answer (IMO it's already superior since OP is asking for something to replace std::cout, but 40ish voters appear not to agree). – Kyle Strand Mar 9 '15 at 21:00
QTextStream qStdout() { return {stdout}; } might be a useful way to wrap this, consistent with qWarning() etc. And maybe some static state to avoid temporary streamage? – Yakk Mar 10 '15 at 19:18
--------------------------------------------------------------
Add this to your project file:
CONFIG += console
------------------------------------------
up vote 22 down vote
Writing to stdout
If you want something that, like std::cout, writes to your application's standard output, you can simply do the following (credit to CapelliC):
QTextStream(stdout) << "string to print" << endl;
https://stackoverflow.com/questions/20497734/qt5-opens-console-with-gui-applications
I use Qt5.2 (git) with MSVC13 on Windows. If I build any application (even the templates) it always opens in a cmd.exe window. I tried different Qt versions and different compilers (even MinGW), but I always have the same problem. If however, I start the application using the green play button in QtDesigner it opens without a cmd window. As suggested here I tried "CONFIG -= console" with no effect. Also I dont use testlib. In my qmake.conf I see the line "QMAKE_LFLAGS_WINDOWS = /SUBSYSTEM:WINDOWS"
Does this depend on the dll files I put in the application directory? QtDesigner does not copy any dlls to the build folder, however it sets some environment variables. To run the application from a separate folder I copy the corresponding dlls from the Qt lib folder into the applications exe folder.
Any ideas for this strange behaviour?
In MSVC 2013 go to your project properties/ linker and change from:
/SUBSYSTEM:CONSOLE
To:
/SUBSYSTEM:WINDOWS
It worked for me.

Ok, this took a while, but i finally found the solution here: Hide console of Windows Application
I had to replace the main entry function:
Replace the following code:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// your code*
}
by
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
{
int argc = 0;
QApplication app( argc, 0 );
}
Thanks everybody!


浙公网安备 33010602011771号