[Development] Cake and eating it too for qDebug printing to system log - https://codereview.qt-project.org/89357

Joerg Bornemann joerg.bornemann at digia.com
Fri Jul 11 14:47:04 CEST 2014


On 11-Jul-14 14:14, Joerg Bornemann wrote:

> Here's the code. Maybe I missed something.

Ossi showed me what I missed.
We cannot just change stdout unconditionally.
So no reason to not do this, as long as the application call is prefixed 
with "start /wait" to run it in the foreground.

---snip---
#include <QApplication>
#include <qt_windows.h>
#include <cstdio>
#include <fcntl.h>
#include <io.h>

int main(int argc, char *argv[])
{
     QApplication a(argc, argv);
     HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
     if (h == INVALID_HANDLE_VALUE || h == NULL) {
         if (!AttachConsole(ATTACH_PARENT_PROCESS))
             return 1;
         h = GetStdHandle(STD_OUTPUT_HANDLE);
         int fd = _open_osfhandle((intptr_t)h, _O_TEXT);
         if (fd <= 0)
             return 2;
         *stdout = *_fdopen(fd, "w");
         setvbuf(stdout, NULL, _IONBF, 0 );
     }
     printf("Hello World from GUI app!\n");
     QMetaObject::invokeMethod(&a, "quit", Qt::QueuedConnection);
     return a.exec();
}
---snap---


Joerg



More information about the Development mailing list