Console output in a Qt GUI app?

前端 未结 17 1741
星月不相逢
星月不相逢 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条回答
  •  萌比男神i
    2020-11-28 07:49

    So many answers to this topic. 0.0

    So I tried it with Qt5.x from Win7 to Win10. It took me some hours to have a good working solution which doesn't produce any problems somewhere in the chain:

    #include "mainwindow.h"
    
    #include 
    
    #include 
    #include 
    #include 
    
    //
    // Add to project file:
    // CONFIG += console
    //
    
    int main( int argc, char *argv[] )
    {
        if( argc < 2 )
        {
        #if defined( Q_OS_WIN )
            ::ShowWindow( ::GetConsoleWindow(), SW_HIDE ); //hide console window
        #endif
            QApplication a( argc, argv );
            MainWindow *w = new MainWindow;
            w->show();
            int e = a.exec();
            delete w; //needed to execute deconstructor
            exit( e ); //needed to exit the hidden console
            return e;
        }
        else
        {
            QCoreApplication a( argc, argv );
            std::string g;
            std::cout << "Enter name: ";
            std::cin >> g;
            std::cout << "Name is: " << g << std::endl;
            exit( 0 );
            return a.exec();
        }
    }
    


    I tried it also without the "CONFIG += console", but then you need to redirect the streams and create the console on your own:

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

    BUT this only works if you start it through a debugger, otherwise all inputs are directed towards the system too. Means, if you type a name via std::cin the system tries to execute the name as a command. (very strange)

    Two other warnings to this attempt would be, that you can't use ::FreeConsole() it won't close it and if you start it through a console the app won't close.



    Last there is a Qt help section in QApplication to this topic. I tried the example there with an application and it doesn't work for the GUI, it stucked somewhere in an endless loop and the GUI won't be rendered or it simply crashes:

    QCoreApplication* createApplication(int &argc, char *argv[])
    {
        for (int i = 1; i < argc; ++i)
            if (!qstrcmp(argv[i], "-no-gui"))
                return new QCoreApplication(argc, argv);
        return new QApplication(argc, argv);
    }
    
    int main(int argc, char* argv[])
    {
        QScopedPointer app(createApplication(argc, argv));
    
        if (qobject_cast(app.data())) {
           // start GUI version...
        } else {
           // start non-GUI version...
        }
    
        return app->exec();
    }
    


    So if you are using Windows and Qt simply use the console option, hide the console if you need the GUI and close it via exit.

提交回复
热议问题