Running a Qt Console Application



  • Hello,

    I have a simple console application that has some header files, source files (.c) and some .lib files that I was able to successfully compile and run in Visual Studio 2010 Express Edition. It simply writes some messages to the terminal, waits for some voice input, process the input and quits after pressing the Q key , etc..

    I would like to integrate those functions into my Project and I need to use QtCreator. For this I first tried to get the same application running in QtCreator (choosing Qt Console Application) by adding the header and source files and linking the library.

    It built just fine, but when I execute the program, I just got the terminal window with the message "Please RETURN to close this window", and nothing else.
    Then I tried starting from scratch and just having a Qt Console Application and one main.cpp file with :

    @
    #include <iostream>

    int main()
    {
    std::cout << "TESTING console in Qt ... " << std::endl;
    std::cout << "TESTING console in Qt ... " << std::endl;
    std::cout << "TESTING console in Qt ... " << std::endl;
    std::cout << "TESTING console in Qt ... " << std::endl;

    return 0;
    

    }
    @

    It worked as expected printing the 4 lines to the terminal that pops up when executing. Then I just added the source files and linked to the library files without changing my main.cpp and same thing happened as before. The application compiles with no problem but when executing the debug lines are not being printed to the terminal (or anything else I try to do in the main function gets "ignored").

    Any ideas of what needs to be done to get this working properly ? Is this because the source files I add are c code and not c++ perhaps?
    I would really appreciate any help on this.

    Thank you very much!



  • Ok, solved this by just going to the "projects" tab on the far left of QtCreator and removing the tick "run in terminal". and running the application. Then if you re set the tick it will work fine in the terminal as well.


  • Moderators

    sergex: That does not sound right. It should either work or not work, but working after doing some magic is just wrong:).

    Could you please "file a bug":https://bugreports.qt-project.org/ report about this?

    Do you have "CONFIG += console" somewhere in your .pro-file? That seems to be important on windows.



  • [quote author="Tobias Hunger" date="1370271585"]Do you have "CONFIG += console" somewhere in your .pro-file? That seems to be important on windows.[/quote]

    Indeed. If you compile a Windows binary with subsystem "Windows" rather than "Console", then the program won't have a console attached and its STDOUT/STDERR will go to nowhere. Launching the application from a console doesn't change anything about that. The STDOUT/STDERR still goes nowhere.

    However, applications compiled with subsystem "Windows" may still request a console at runtime via AllocConsole(). But even then STDOUT or STDERR will not work right away! That's because the C-Runtime already was initialized before you request the console. So if you want printf() and cout work as usual, you must first call AllocConsole() and then fix STDOUT/STDERR to actually point to the newly allocated console...

    You can use something like this:
    http://pastie.org/private/pl0hfrnrapd7f88rosmqa



  • Thanks for the tips, I'll file a bug report then.
    Yes, I did have CONFIG += console in my .pro file when I was having the problem.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.