問題描述
我有一個(gè)在 Windows 上運(yùn)行的 Qt GUI 應(yīng)用程序,它允許傳遞命令行選項(xiàng),在某些情況下我想向控制臺(tái)輸出一條消息然后退出,例如:
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();
}
但是,當(dāng)我從命令提示符運(yùn)行應(yīng)用程序時(shí),控制臺(tái)消息不會(huì)出現(xiàn).有誰知道我怎樣才能讓它發(fā)揮作用?
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 并不真正支持雙模式應(yīng)用程序.
Windows does not really support dual mode applications.
要查看控制臺(tái)輸出,您需要?jiǎng)?chuàng)建一個(gè)控制臺(tái)應(yīng)用程序
To see console output you need to create a console application
CONFIG += console
但是,如果您雙擊該程序以啟動(dòng) GUI 模式版本,則會(huì)出現(xiàn)一個(gè)控制臺(tái)窗口,這可能不是您想要的.為了防止控制臺(tái)窗口出現(xiàn),您必須創(chuàng)建一個(gè) GUI 模式的應(yīng)用程序,在這種情況下,您將不會(huì)在控制臺(tái)中獲得任何輸出.
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.
一個(gè)想法可能是創(chuàng)建第二個(gè)小應(yīng)用程序,它是一個(gè)控制臺(tái)應(yīng)用程序并提供輸出.這樣就可以調(diào)用第二個(gè)來做工作了.
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.
或者您可以將所有功能放在一個(gè) DLL 中,然后創(chuàng)建兩個(gè)版本的 .exe 文件,它們具有調(diào)用 DLL 的非常簡單的主要函數(shù).一種用于 GUI,一種用于控制臺(tái).
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.
這篇關(guān)于Qt GUI 應(yīng)用程序中的控制臺(tái)輸出?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!