我有一个在 Windows 上运行的 Qt GUI 应用程序,它允许传递命令行选项,在某些情况下我想向控制台输出一条消息然后退出,例如:
I have a Qt GUI application running on Windows that allows command-line options to be passed and under some circumstances I want to output a message to the console and then quit, for example:
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();
}
但是,当我从命令提示符运行应用程序时,控制台消息不会出现.有谁知道我怎样才能让它发挥作用?
However, the console messages do not appear when I run the app from a command-prompt. Does anyone know how I can get this to work?
Windows 并不真正支持双模式应用程序.
Windows does not really support dual mode applications.
要查看控制台输出,您需要创建一个控制台应用程序
To see console output you need to create a console application
CONFIG += console
但是,如果您双击该程序以启动 GUI 模式版本,则会出现一个控制台窗口,这可能不是您想要的.为了防止控制台窗口出现,您必须创建一个 GUI 模式的应用程序,在这种情况下,您将不会在控制台中获得任何输出.
However, if you double click on the program to start the GUI mode version then you will get a console window appearing, which is probably not what you want. To prevent the console window appearing you have to create a GUI mode application in which case you get no output in the console.
一个想法可能是创建第二个小应用程序,它是一个控制台应用程序并提供输出.这样就可以调用第二个来做工作了.
One idea may be to create a second small application which is a console application and provides the output. This can call the second one to do the work.
或者您可以将所有功能放在一个 DLL 中,然后创建两个版本的 .exe 文件,它们具有调用 DLL 的非常简单的主要函数.一种用于 GUI,一种用于控制台.
Or you could put all the functionality in a DLL then create two versions of the .exe file which have very simple main functions which call into the DLL. One is for the GUI and one is for the console.
这篇关于Qt GUI 应用程序中的控制台输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!