简体   繁体   English

C ++-Qt-Visual Studio 2010-具有GUI和控制台的应用程序

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

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. 如果未给程序提供参数,则它将作为GUI应用程序启动;如果给定参数,则将通过命令行运行。 I was able to get visual studio to display and print to the console with Properties>Linker>SubSystem (Console/SUBSYSTEM:CONSOLE), but this makes it so that the console always displays when the application is launched, how can I selectively display the console so that when the app is run with the GUI it does not appear. 我可以使用Properties> Linker> SubSystem(Console / SUBSYSTEM:CONSOLE)来使Visual Studio显示和打印到控制台,但这使控制台始终在启动应用程序时显示,如何有选择地显示控制台,这样当应用程序通过GUI运行时就不会出现。 I have looked through the site, but all I have found is how to set it to only be a windows application, and I need it to function as both 我浏览了该站点,但发现的只是如何将其设置为仅Windows应用程序,而我需要它既能同时充当

Thanks!!! 谢谢!!!

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. 没有参数也没有窗口调用它。

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. 我没有用过Qt,但是您应该可以将其粘贴到某个地方并使它工作。

Edit: added the headers needed 编辑:添加所需的标题

您可以使用Windows子系统来使用它,并在需要控制台且应用程序也带有gui的情况下调用AllocConsole

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM