C++ - Qt - Visual Studio 2010 - application with both gui and console

前端 未结 3 2059
名媛妹妹
名媛妹妹 2021-01-01 07:14

If no arguments are given to the program it launches as a GUI application, if it is given args it is run through the command line. I was able to get visual studio to displa

3条回答
  •  感动是毒
    2021-01-01 08:01

    Here's some code I had lying around that creates a console and attaches input and output to it:

    #include 
    #include 
    #include 
    #include 
    
    void Console::createConsole()
    {
        AllocConsole();
        SetConsoleTitle("Debug console");
    
        int hConHandle;
        long lStdHandle;
    
        FILE *fp;   // redirect unbuffered STDOUT to the console
        lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
        hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
        fp = _fdopen( hConHandle, "w" );
        *stdout = *fp;
        setvbuf( stdout, NULL, _IONBF, 0 ); 
    
        // redirect unbuffered STDIN to the console
        lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
        hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
        fp = _fdopen( hConHandle, "r" );
        *stdin = *fp;
        setvbuf( stdin, NULL, _IONBF, 0 );  
    
        // redirect unbuffered STDERR to the console
        lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
        hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
        fp = _fdopen( hConHandle, "w" );
        *stderr = *fp;
        setvbuf( stderr, NULL, _IONBF, 0 );
    }
    

    I haven't used Qt but you should be able to stick that somewhere and make it work.

    Edit: added the headers needed

提交回复
热议问题