Console output in a Qt GUI app?

前端 未结 17 1721
星月不相逢
星月不相逢 2020-11-28 07:06

I have a Qt GUI application running on Windows that allows command-line options to be passed and under some circumstances I want to output a message to the console and then

17条回答
  •  遥遥无期
    2020-11-28 08:07

    I also played with this, discovering that redirecting output worked, but I never saw output to the console window, which is present for every windows application. This is my solution so far, until I find a Qt replacement for ShowWindow and GetConsoleWindow.

    Run this from a command prompt without parameters - get the window. Run from command prompt with parameters (eg. cmd aaa bbb ccc) - you get the text output on the command prompt window - just as you would expect for any Windows console app.

    Please excuse the lame example - it represents about 30 minutes of tinkering.

    #include "mainwindow.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    
    QT_USE_NAMESPACE
    
    int main(int argc, char *argv[])
    {
        if (argc > 1)   {
            // User has specified command-line arguments
            QCoreApplication a(argc, argv);
            QTextStream  out(stdout);
            int     i;
    
            ShowWindow (GetConsoleWindow(),SW_NORMAL);
            for (i=1; i

提交回复
热议问题