Console output in a Qt GUI app?

前端 未结 17 1618
星月不相逢
星月不相逢 2020-11-28 07:06

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

相关标签:
17条回答
  • 2020-11-28 08:08

    No way to output a message to console when using QT += gui.

    fprintf(stderr, ...) also can't print output.

    Use QMessageBox instead to show the message.

    0 讨论(0)
  • 2020-11-28 08:08

    It may have been an oversight of other answers, or perhaps it is a requirement of the user to indeed need console output, but the obvious answer to me is to create a secondary window that can be shown or hidden (with a checkbox or button) that shows all messages by appending lines of text to a text box widget and use that as a console?

    The benefits of such a solution are:

    • A simple solution (providing all it displays is a simple log).
    • The ability to dock the 'console' widget onto the main application window. (In Qt, anyhow).
    • The ability to create many consoles (if more than 1 thread, etc).
    • A pretty easy change from local console output to sending log over network to a client.

    Hope this gives you food for thought, although I am not in any way yet qualified to postulate on how you should do this, I can imagine it is something very achievable by any one of us with a little searching / reading!

    0 讨论(0)
  • 2020-11-28 08:08

    Make sure Qt5Core.dll is in the same directory with your application executable.

    I had a similar issue in Qt5 with a console application: if I start the application from Qt Creator, the output text is visible, if I open cmd.exe and start the same application there, no output is visible. Very strange!

    I solved it by copying Qt5Core.dll to the directory with the application executable.

    Here is my tiny console application:

    #include <QCoreApplication>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        int x=343;
        QString str("Hello World");
        qDebug()<< str << x<<"lalalaa";
    
        QTextStream out(stdout);
        out << "aldfjals alsdfajs...";
    }
    
    0 讨论(0)
  • 2020-11-28 08:08

    One solution is to run powershell and redirect the output to whatever stream you want.

    Below is an example of running powershell from cmd.exe and redirecting my_exec.exe output to both the console and an output.txt file:

    powershell ".\my_exec.exe | tee output.txt"
    
    0 讨论(0)
  • 2020-11-28 08:08

    First of all you can try flushing the buffer

    std::cout << "Hello, world!"<<std::endl;
    

    For more Qt based logging you can try using qDebug.

    0 讨论(0)
提交回复
热议问题