Console output in a Qt GUI app?

前端 未结 17 1619
星月不相逢
星月不相逢 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 07:54

    Windows does not really support dual mode applications.

    To see console output you need to create a console application

    CONFIG += console
    

    However, if you double click on the program to start the GUI mode version then you will get a console window appearing, which is probably not what you want. To prevent the console window appearing you have to create a GUI mode application in which case you get no output in the console.

    One idea may be to create a second small application which is a console application and provides the output. This can call the second one to do the work.

    Or you could put all the functionality in a DLL then create two versions of the .exe file which have very simple main functions which call into the DLL. One is for the GUI and one is for the console.

    0 讨论(0)
  • 2020-11-28 08:01

    Something you may want to investigate, at least for windows, is the AllocConsole() function in the windows api. It calls GetStdHandle a few times to redirect stdout, stderr, etc. (A quick test shows this doesn't entirely do what we want it to do. You do get a console window opened alongside your other Qt stuff, but you can't output to it. Presumably, because the console window is open, there is some way to access it, get a handle to it, or access and manipulate it somehow. Here's the MSDN documentation for those interested in figuring this out:

    AllocConsole(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms681944%28v=vs.85%29.aspx

    GetStdHandle(...): http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx

    (I'd add this as a comment, but the rules prevent me from doing so...)

    0 讨论(0)
  • 2020-11-28 08:04

    In your .pro add

    CONFIG          += console
    
    0 讨论(0)
  • 2020-11-28 08:06

    Add:

    #ifdef _WIN32
    if (AttachConsole(ATTACH_PARENT_PROCESS)) {
        freopen("CONOUT$", "w", stdout);
        freopen("CONOUT$", "w", stderr);
    }
    #endif
    

    at the top of main(). This will enable output to the console only if the program is started in a console, and won't pop up a console window in other situations. If you want to create a console window to display messages when you run the app outside a console you can change the condition to:

    if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
    
    0 讨论(0)
  • 2020-11-28 08:07
    void Console()
    {
        AllocConsole();
        FILE *pFileCon = NULL;
        pFileCon = freopen("CONOUT$", "w", stdout);
    
        COORD coordInfo;
        coordInfo.X = 130;
        coordInfo.Y = 9000;
    
        SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coordInfo);
        SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE),ENABLE_QUICK_EDIT_MODE| ENABLE_EXTENDED_FLAGS);
    }
    
    int main(int argc, char *argv[])
    {
        Console();
        std::cout<<"start@@";
        qDebug()<<"start!";
    

    You can't use std::cout as others have said,my way is perfect even for some code can't include "qdebug" !

    0 讨论(0)
  • 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 <QTextStream>
    #include <QCoreApplication>
    #include <QApplication>
    #include <QWidget>
    #include <windows.h>
    
    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<argc; i++)
                 out << i << ':' << argv [i] << endl;
            out << endl << "Hello, World" << endl;
            out << "Application Directory Path:" << a.applicationDirPath() << endl;
            out << "Application File Path:" << a.applicationFilePath() << endl;
            MessageBox (0,(LPCWSTR)"Continue?",(LPCWSTR)"Silly Question",MB_YESNO);
            return 0;
        } else  {
            QApplication a(argc, argv);
            MainWindow w;
    
            w.setWindowTitle("Simple example");
            w.show();
            return a.exec();
        }
    }
    
    0 讨论(0)
提交回复
热议问题