开发者

Non Qt console app in Qt Creator

开发者 https://www.devze.com 2023-03-10 08:38 出处:网络
I want to create the simple console app below in Qt Creator: #include <iostream> int main(int argc, char* argv[])

I want to create the simple console app below in Qt Creator:

#include <iostream>

int main(int argc, char* argv[])
{
   std::cout << "Hello WOrld";

   return 0;
}

I've seen so开发者_如何学Cme possible duplicates on SO, I have ticked the "Run in Terminal" option in Run Settings. A console window does pop up on CTRL+R, but it does not display "Hello World", simply "Press Enter to exit".

The above is by creating an Empty Project.

I have tried creating a "Qt Console Application" which generates the code below. This does work fine, but I want the simple non Qt version above.

#include <QtCore/QCoreApplication>
#include <iostream>

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

    std::cout << "Hello World";

   return a.exec();
}


Besides ticking "Run in Terminal" you need to add "CONFIG += console" to your .pro file (if you are using qmake).

TEMPLATE = app

CONFIG += console

SOURCES += main.cpp


After trying Qt again after a long time, it now works. The project file has "CONFIG -=qt" by default. I'm not sure if this alone would have solved the problem back then, but it is the only difference I can see.

Full .pro file:

TEMPLATE = app
CONFIG += console
CONFIG -= qt

SOURCES += main.cpp


The only fault I can see with that example is that the stream is not flushed (please change the std::cout line to:

std::cout << "Hello World" << std::endl;

However, that is unlikely to be the issue you have, although the following example that I've found at http://www.richelbilderbeek.nl/CppQtHelloWorldConsole.htm implies that it is indeed a buffer handling issue where QtCreator makes some assumptions regarding buffering. (Note that that url adds a std::cin.get() call, which forces the application to pause, and thus, you should certainly see some output).


If you stumbled over this thread, because your application instantly exits and the console just shows "Press enter to exit":

This is how your application behaves, if you launch it from QtCreator and it can't find dependent DLLs (very unhelpful, by the way). To find out what exactly is missing, you can start your application without QtCreator. Dependent DLLs have to be in one of the locations listed here http://msdn.microsoft.com/de-de/library/7d83bc18.aspx.

0

精彩评论

暂无评论...
验证码 换一张
取 消