Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QCoreApplication



  • Hi!

    Can anyone explain this to me...

    I have the following code..

    #include <QCoreApplication>
    #include <QDebug>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    qDebug() << "Test";
    return a.exec();
    }

    From the console I run:
    qmake -project
    qmake
    make
    ./app

    On Ubuntu this outputs "Test", but on CentOS the program just sits there with no output.
    If I remove QCoreApplication it outputs "Test" as well, why is that?


  • Qt Champions 2019

    @Willys It's because of

    return a.exec();
    

    This line starts the Qt event loop. Your qDebug output is called before that line.
    Try

    qDebug() << "Test" << flush;
    


  • @jsulm said in QCoreApplication:

    @Willys It's because of

    return a.exec();
    

    This line starts the Qt event loop. Your qDebug output is called before that line.
    Try

    qDebug() << "Test" << flush;
    

    Hi! Thank you for the reply.
    I got the same result I'm afraid.

    Some more info:
    Works on Ubuntu 18.04, not working on CentOS 7.5.

    .pro file looks like this:

    QT -= gui

    TEMPLATE = app
    TARGET = test
    INCLUDEPATH += .
    CONFIG += c+11 console

    SOURCES += main.cpp


  • Qt Champions 2017

    may look simple. Just try to put endl at the end of debug. It may be just a buffer issue ?



  • @Willys said in QCoreApplication:

    Unlike other posters I do not believe this will be a "flushing" problem. qDebug() should not be buffering when it writes its output (though of course you should try their suggestions).

    On Ubuntu this outputs "Test", but on CentOS the program just sits there with no output.
    If I remove QCoreApplication it outputs "Test" as well, why is that?

    Do you mean you completely delete the QCoreApplication a(argc, argv); line and do not replace it with anything else? I think you do.

    Rather than the qDebug() not working, it may be/sounds like CentOS is not returning from QCoreApplication a(argc, argv); constructor at all? Either step through code in debugger to see if this is the case, or if you can't do that try something just like

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        fprintf(stderr, "Test\n");
        return 0;
    }
    

    (add necessary includes for stdio or use cout << ... or something).


  • Lifetime Qt Champion

    It may also be that the qDebug output is redirected to some logfile...



  • @JonB said in QCoreApplication:

    @Willys said in QCoreApplication:

    Unlike other posters I do not believe this will be a "flushing" problem. qDebug() should not be buffering when it writes its output (though of course you should try their suggestions).

    On Ubuntu this outputs "Test", but on CentOS the program just sits there with no output.
    If I remove QCoreApplication it outputs "Test" as well, why is that?

    Do you mean you completely delete the QCoreApplication a(argc, argv); line and do not replace it with anything else? I think you do.

    Rather than the qDebug() not working, it may be/sounds like CentOS is not returning from QCoreApplication a(argc, argv); constructor at all? Either step through code in debugger to see if this is the case, or if you can't do that try something just like

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        fprintf(stderr, "Test\n");
        return 0;
    }
    

    (add necessary includes for stdio or use cout << ... or something).

    Yes, that works.
    Although if I'm gonna make a console app with QT's classes, signal and slots I need QCoreApplication and return a.exec() if I understand correct?


  • Moderators

    @Willys this actually confirms, or at least strongy hints, that @aha_1980 suggestion is correct, QDebug seems to redirect the output somehwere else and fprintf prints directly to the console.



  • @Willys
    The it looks like this is a qDebug()-only issue for you to investigate.



  • @aha_1980 said in QCoreApplication:

    It may also be that the qDebug output is redirected to some logfile...

    That was actually my first thought before I asked this question but I can't seems to find it anywhere.

    But you seems to be right. I now tried:

    #include <QCoreApplication>
    #include <QFile>
    #include <iostream>

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

    QFile file("/tmp/test");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        std::cout << "Could not open" << file.fileName().toStdString();
        return 1;
    }
    
    while(!file.atEnd()) {
        std::cout << file.readLine().toStdString();
    }
    
    return a.exec();
    

    }

    and it works.

    So guess I was way off and need to investigate qDebug() on CentOS like JonB said.

    Thanks guys!



  • @Willys
    I hate to suggest this, but are you sure your CentOS version is even compiled for debug...?! Isn't qDebug() a NOOP under Release flags?

    P.S.
    Oh, under CentOS put in an additonal qDebug() before QCoreApplication a(argc, argv);, as well as after. Any difference?



  • @JonB said in QCoreApplication:

    @Willys
    I hate to suggest this, but are you sure your CentOS version is even compiled for debug...?! Isn't qDebug() a NOOP under Release flags?

    P.S.
    Oh, under CentOS put in an additonal qDebug() before QCoreApplication a(argc, argv);, as well as after. Any difference?

    It's just a default installation with the minimal ISO.

    Putting qDebug() << "Test" before QCoreApplication a(argc, argv); works. o.O
    Just found out that qInfo() works after QCoreApp as well.




Log in to reply