std::cout in Qt windows application
Hello,
I'm developing a Qt windows application with a QMainWindow instance as the main window.
My question is: If I launch the application from the command prompt or PowerShell, is there a way to print information to that command prompt?
I'm adding support to execute Lua scripts, and I added a command line argument that if present, executes a script without displaying the QMainWindow. Something like:
> MyApp.exe
Opens MyApp with QMainWindow ui.
> MyApp.exe -s test.lua
Executes the Lua script.
This works, but if there are errors or print calls in the script, I'd like to be able to print them in the command prompt.
I have a parallel application that loads the same libraries as MyApp, and it works. Something like this:
> MyAppScript.exe test.lua
But for simplicity, I'd like to be able to execute and display these messages in MyApp.
I've read that with Qt it's possible to open the QMainApplication and a console window, but I'd rather keep the output in the command shell where the exe was called from.
Thank you!
Re: std::cout in Qt windows application
Have you tested to see if writing to cout from your MainWindow instance (probably after the showEvent() to be sure everything is fully running) produces any output to the command shell? If that works, it could be that your Lua interpreter is eating the output and you need to redirect that.
Re: std::cout in Qt windows application
Quote:
Originally Posted by
d_stranz
Have you tested to see if writing to cout from your MainWindow instance (probably after the showEvent() to be sure everything is fully running) produces any output to the command shell? If that works, it could be that your Lua interpreter is eating the output and you need to redirect that.
Hello d_stranz.
Yes, I've tested with cout, cerr, printf.. nothing prints to the command prompt.
My understanding is that the linker links either with Windows (/SUBSYSTEM:WINDOWS) or Console (/SUBSYSTEM:CONSOLE). I use CMake to build my projects, and my application links with windows:
ADD_EXECUTABLE( ${APP_MAIN} WIN32 ${FILES} )
I don't know if cout and printf have empty definitions when linking with Windows, but nothing shows off.
Thank you for your reply!
Re: std::cout in Qt windows application
You have to link with /SUBSYSTEM:CONSOLE.
Re: std::cout in Qt windows application
Quote:
Originally Posted by
Lesiok
You have to link with /SUBSYSTEM:CONSOLE.
Which means you have to remove 'WIN32' from ADD_EXECUTABLE call as explained in the doc: https://cmake.org/cmake/help/latest/...xecutable.html and https://cmake.org/cmake/help/latest/...N32_EXECUTABLE
Re: std::cout in Qt windows application
Good to know. I am starting to switch over to CMake for building all of my apps so I can have consistent builds instead of having to manually sync up numerous independent builds of Visual Studio solutions and projects.
Re: std::cout in Qt windows application
Oh, interesting. I removed the WIN32 option in CMake's ADD_EXECUTABLE and this created the project with SubSystem CONSOLE. When the application is launched, this opens a Console window, and immediately after my Qt ui
I don't want to have a console in the background that can be closed by mistake and kills the main Qt application, but this gives me new things to investigate. For starters I found this:
https://www.devever.net/~hl/win32con
Thank you all for your help!