Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt GUI应用程序中的控制台输出?_C++_Windows_Qt_Qt4 - Fatal编程技术网

C++ Qt GUI应用程序中的控制台输出?

C++ Qt GUI应用程序中的控制台输出?,c++,windows,qt,qt4,C++,Windows,Qt,Qt4,我有一个运行在Windows上的Qt GUI应用程序,它允许传递命令行选项,在某些情况下,我希望向控制台输出消息,然后退出,例如: int main(int argc, char *argv[]) { QApplication a(argc, argv); if (someCommandLineParam) { std::cout << "Hello, world!"; return 0; } MainWindow w; w.show();

我有一个运行在Windows上的Qt GUI应用程序,它允许传递命令行选项,在某些情况下,我希望向控制台输出消息,然后退出,例如:

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);

  if (someCommandLineParam)
  {
    std::cout << "Hello, world!";
    return 0;
  }

  MainWindow w;
  w.show();

  return a.exec();
}
intmain(intargc,char*argv[])
{
质量保证申请a(argc、argv);
if(someCommandLineParam)
{

std::cout首先,您可以尝试刷新缓冲区

std::cout << "Hello, world!"<<std::endl;

std::cout我在我的项目中使用了下面的标题。希望对您有所帮助

#ifndef __DEBUG__H
#define __DEBUG__H

#include <QtGui>    

static void myMessageOutput(bool debug, QtMsgType type, const QString & msg) {

    if (!debug) return;

    QDateTime dateTime = QDateTime::currentDateTime();
    QString dateString = dateTime.toString("yyyy.MM.dd hh:mm:ss:zzz");

    switch (type) {

        case QtDebugMsg:
            fprintf(stderr, "Debug: %s\n", msg.toAscii().data());
            break;
        case QtWarningMsg:
            fprintf(stderr, "Warning: %s\n", msg.toAscii().data());
            break;
        case QtCriticalMsg:
            fprintf(stderr, "Critical: %s\n", msg.toAscii().data());
            break;
        case QtFatalMsg:
            fprintf(stderr, "Fatal: %s\n", msg.toAscii().data());
            abort();
    }
}

#endif
\ifndef\uu调试
#定义调试
#包括
静态void myMessageOutput(bool调试、QtMsgType类型、const QString和msg){
如果(!debug)返回;
QDateTime dateTime=QDateTime::currentDateTime();
QString dateString=dateTime.toString(“yyyy.MM.dd hh:MM:ss:zzz”);
开关(类型){
案例QtDebugMsg:
fprintf(stderr,“调试:%s\n”,msg.toAscii().data());
打破
案例QtWarningMsg:
fprintf(stderr,“警告:%s\n”,msg.toAscii().data());
打破
案例QTSG:
fprintf(stderr,“关键:%s\n”,msg.toAscii().data());
打破
案例QTALMSG:
fprintf(stderr,“致命:%s\n”,msg.toAscii().data());
中止();
}
}
#恩迪夫

PS:如果将来需要,您可以将
dateString
添加到输出中。

首先,为什么您需要在发布模式构建中输出到控制台?当有gui时,没有人会想到查看那里

其次,qDebug很花哨:)

第三,您可以尝试将
控制台
添加到
.pro
配置
,它可能会起作用。

在.pro中添加

CONFIG          += console

Windows并不真正支持双模式应用程序

要查看控制台输出,您需要创建一个控制台应用程序

CONFIG += console
但是,如果双击程序以启动GUI模式版本,则会出现控制台窗口,这可能不是您想要的。要防止出现控制台窗口,您必须创建GUI模式应用程序,在这种情况下,控制台中不会有输出

一个想法可能是创建第二个小应用程序,它是一个控制台应用程序并提供输出。这可以调用第二个应用程序来完成工作


或者,您可以将所有功能放在一个DLL中,然后创建两个版本的.exe文件,它们具有调用DLL的非常简单的主要功能。一个用于GUI,另一个用于控制台。

使用
QT+=GUI
时,无法向控制台输出消息

fprintf(stderr,…)
也无法打印输出


使用
QMessageBox
来显示消息。

哦,当使用
QT+=gui
CONFIG+=console
时,您可以输出消息


您需要
printf(“foo-bar”)
但您可能需要研究的是windows api中的alloconsole()函数,至少在windows中是这样。它会多次调用GetStdHandle来重定向stdout、stderr等。(快速测试表明,这并不完全符合我们的要求。您确实会在其他Qt内容旁边打开一个控制台窗口,但无法输出到它。可能是因为控制台窗口打开了,所以有一些方法可以访问它,获取它的句柄,或者以某种方式访问和操作它。以下是MSDN文档供感兴趣的人使用要弄清楚这一点:

AllocConsole():

GetStdHandle(…):

(我想将此添加为注释,但规则阻止我这样做…

void Console()
{
allocsole();
文件*pFileCon=NULL;
pFileCon=freopen(“CONOUT$”,“w”,stdout);
协调信息;
坐标X=130;
坐标信息Y=9000;
设置控制台屏幕缓冲区大小(GetStdHandle(标准输出句柄),坐标信息);
设置控制台模式(GetStdHandle(标准输出句柄),启用快速编辑模式,启用扩展标志);
}
int main(int argc,char*argv[])
{
控制台();

std::cout这可能是对其他答案的疏忽,或者可能是用户确实需要控制台输出的要求,但对我来说显而易见的答案是创建一个可以显示或隐藏的辅助窗口(带有复选框或按钮)通过将文本行附加到文本框小部件并将其用作控制台来显示所有消息

这种解决方案的好处是:

  • 一个简单的解决方案(提供一个简单的日志)
  • 将“控制台”小部件停靠到主应用程序窗口的能力。(无论如何,在Qt中)
  • 能够创建多个控制台(如果有多个线程等)
  • 从本地控制台输出到通过网络向客户端发送日志,这是一个非常简单的更改

希望这能引起您的思考,尽管我还没有资格假设您应该如何做,但我可以想象,只要稍微搜索/阅读一下,我们中的任何人都可以实现这一点!

确保Qt5Core.dll与您的应用程序可执行文件位于同一目录中

我在Qt5中遇到了与控制台应用程序类似的问题: 如果我从Qt Creator启动应用程序,则输出文本可见, 如果我打开cmd.exe并在那里启动相同的应用程序,则没有可见的输出。 很奇怪

我通过将Qt5Core.dll复制到包含应用程序可执行文件的目录中解决了这个问题

这是我的微型控制台应用程序:

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    int x=343;
    QString str("Hello World");
    qDebug()<< str << x<<"lalalaa";

    QTextStream out(stdout);
    out << "aldfjals alsdfajs...";
}
#包括
#包括
int main(int argc,char*argv[])
{
int x=343;
QString str(“你好世界”);

qDebug()我也玩过这个,发现重定向输出是有效的,但我从来没有看到控制台窗口的输出,每个windows应用程序都有控制台窗口。到目前为止,这是我的解决方案,直到我找到了ShowWindow和GetConsoleWindow的Qt替代品

在没有参数的命令提示符下运行-获取窗口。在有参数的命令提示符下运行(例如cmd aaa bbb ccc)-您可以在命令提示符窗口上获取文本输出-就像您对任何Windo所期望的那样
#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    int x=343;
    QString str("Hello World");
    qDebug()<< str << x<<"lalalaa";

    QTextStream out(stdout);
    out << "aldfjals alsdfajs...";
}
#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();
    }
}
CONFIG   += console
#ifdef _WIN32
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);
}
#endif
if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
QT +=widgets
CONFIG += console
TARGET = minimal
SOURCES += \ main.cpp
#include <QApplication>
#include <QtCore>

using namespace  std;

QTextStream in(stdin);
QTextStream out(stdout);

int main(int argc, char *argv[]){
QApplication app(argc,argv);
qDebug() << "Please enter some text over here: " << endl;
out.flush();
QString input;
input = in.readLine();
out << "The input is " << input  << endl;
return app.exec();
}
#include <QApplication>
#include <QtCore>

using namespace  std;

QTextStream in(stdin);
QTextStream out(stdout);

int main(int argc, char *argv[]){
    QApplication app(argc,argv);
    qDebug() << "Please enter some text over here: " << endl;
    out.flush();
    QString input;
    do{
        input = in.readLine();
        if(input.size()==6){
            out << "The input is " << input  << endl;   
        }
        else
        {
            qDebug("Not the exact input man");
        }
    }while(!input.size()==0);

    qDebug(" WE ARE AT THE END");

    // endif
    return app.exec();
} // end main
#include "mainwindow.h"

#include <QApplication>

#include <windows.h>
#include <stdio.h>
#include <iostream>

//
// 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();
    }
}
#ifdef _WIN32
if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole()){
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);
    freopen("CONIN$", "r", stdin);
}
#endif
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<QCoreApplication> app(createApplication(argc, argv));

    if (qobject_cast<QApplication *>(app.data())) {
       // start GUI version...
    } else {
       // start non-GUI version...
    }

    return app->exec();
}
powershell ".\my_exec.exe | tee output.txt"