Results 1 to 7 of 7

Thread: std::cout in Qt windows application

  1. #1
    Join Date
    Nov 2014
    Posts
    35
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default 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!
    pixaeiro
    http://www.pixaflux.com
    Non-destructive Image Editor

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2014
    Posts
    35
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: std::cout in Qt windows application

    Quote Originally Posted by d_stranz View Post
    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!
    pixaeiro
    http://www.pixaflux.com
    Non-destructive Image Editor

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std::cout in Qt windows application

    You have to link with /SUBSYSTEM:CONSOLE.

  5. #5
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: std::cout in Qt windows application

    Quote Originally Posted by Lesiok View Post
    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

  6. The following user says thank you to ChristianEhrlicher for this useful post:

    d_stranz (13th February 2021)

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #7
    Join Date
    Nov 2014
    Posts
    35
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default 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!
    pixaeiro
    http://www.pixaflux.com
    Non-destructive Image Editor

Similar Threads

  1. Replies: 2
    Last Post: 14th April 2016, 21:43
  2. cout to stdout in Qt Console Application
    By tuli in forum Qt Programming
    Replies: 3
    Last Post: 6th March 2013, 13:51
  3. cout in Qt
    By qt_developer in forum Newbie
    Replies: 3
    Last Post: 25th May 2012, 23:02
  4. where is cout
    By hqking1988 in forum General Programming
    Replies: 3
    Last Post: 22nd August 2010, 04:35
  5. Replies: 3
    Last Post: 12th July 2010, 07:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.