問題描述
我有一個在 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 應用程序中的控制臺輸出?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!