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

前端 未结 3 2030
名媛妹妹
名媛妹妹 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 07:59

    This works I guess:

    #include <QtGui/QApplication>
    #include <QtGui/QMainWindow>
    
    
    int
    main(int n_app_args, char **app_arg)
    {
        QCoreApplication * application = 0;
    
        if ( n_app_args == 1 )
        {
            application = new QCoreApplication(n_app_args, app_arg);
        }
        else
        {
            application = new QApplication(n_app_args, app_arg);
            QMainWindow * mainWindow = new QMainWindow();
            mainWindow->show();
        }
    
    
        return application->exec();
    }
    

    Call it with an argument and you get a little (empty) window. Call it with no argument and no window.

    0 讨论(0)
  • 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 <Windows.h>
    #include <stdio.h>
    #include <io.h>
    #include <fcntl.h>
    
    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

    0 讨论(0)
  • 2021-01-01 08:10

    You can have it using the windows subsystem, and calling AllocConsole when you need a console while the application has a gui as well.

    0 讨论(0)
提交回复
热议问题