在 Windows 上使用 Qt Creator 的控制台应用程序:在关闭控制台之前等待

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1356367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:04:17  来源:igfitidea点击:

Console app with Qt Creator on Windows : wait before closing the console

cwindowsqt-creator

提问by Jér?me

I'm running a very simple console app on Windows with Qt Creator.

我正在使用 Qt Creator 在 Windows 上运行一个非常简单的控制台应用程序。

When launching it, the dos console is openned, my output is displayed, but then the app terminates and the console immediately closes.

启动它时,dos 控制台打开,显示我的输出,但随后应用程序终止,控制台立即关闭。

How can I make sure the console will stay open until the user presses a key ?

如何确保控制台将保持打开状态,直到用户按下某个键?

采纳答案by Jér?me

Since Qt Creator 1.3.0, it's much easier :

从 Qt Creator 1.3.0 开始,就简单多了:

Go to the projecttab (on the left) to edit the project's setting.

转到project选项卡(在左侧)以编辑项目的设置。

In the section Run Settings, clic on Show detailsand check the Run in Terminalcheckbox.

在部分中Run Settings,单击Show details并选中Run in Terminal复选框。

Thus, the application will be launched in a console window and the console window will wait until the enter key is pressed before closing.

因此,应用程序将在控制台窗口中启动,控制台窗口将等到按下 Enter 键后再关闭。

No need to add some lines to the code anymore !

不再需要在代码中添加一些行了!

回答by Matthieu

Here are two solutions :

这里有两个解决方案:

#include <QTextStream>
#include <QFile>
//#include <conio.h> // for getch()

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

    // JC and friends code

    // Qt Solution
    QTextStream Qin(stdin);
    forever
    {
        QString Line = Qin.readLine();
        if (!Line.isNull())
        {
            break;
        }
    }

    // conio solution
    //getch();
    return 0;
}

Both solutions tested with Qt Creator 1.2.1 on Windows Vista !

这两种解决方案都在 Windows Vista 上使用 Qt Creator 1.2.1 进行了测试!

Hope it helps ;-)

希望能帮助到你 ;-)