[solved] QTimer in console application



  • Hello,

    can i use QTimer in a console application without QObject , Signal/ Slot overhead ?
    I assume i need Signal Slot mechanism to get the timeout event directed to a function (Slot),
    but I 'm not sure if there isn 't a simple solution.

    thx wally


  • Moderators

    In short : No, you can't use a slot without using a slot :)

    If you want to invoke some action after some time then you either need to sleep the thread (blocking) or wait for some time in a loop(non-blocking). Qt has both - thread support and an event loop. A connection from a QTimer is a form of using the second method so there's pretty little you can do in any other way, neither using standard facilities nor 3rd party libs.

    Direct connections in a single threaded app are almost as cheap as plain function calls so the runtime overhead is next to none. What is your concern exactly?



  • Thanks for reply.

    bq. What is your concern exactly?

    nothing special, just wnated a confirmation for my assumption above.
    ("i can not use QTimer efficiently in a single main.cpp without additional classfiles")
    Actually i' m learning about QTimer and i think its a good class for playing around with QThread in next chapter. In the past i often met a much simplier solution as
    my own approach after talking in forum. That's all


  • Moderators

    Oh, I thought you meant runtime efficiency not lines of code count.

    bq. i can not use QTimer efficiently in a single main.cpp without additional classfiles

    Sure you can. This app for example waits for 5s, does stuff, and then quits:
    @
    #include <QApplication>
    #include <QTimer>

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

    QTimer t;
    QObject::connect(&t, &QTimer::timeout, [&](){
        //do stuff
        a.exit();
    });
    t.start(5000);
    
    return a.exec&#40;&#41;;
    

    }
    @

    bq. Actually i’ m learning about QTimer and i think its a good class for playing around with QThread in next chapter.

    QTimer is more of a single threaded construct. The timeout is handled(usually) in the thread the timer lives in. You can handle it in a worker thread of course but there's little point to it. If you want to wait for some time in a worker thread it's better to just sleep.


  • Moderators

    Actually starting with Qt 5.4 (yay, finally!) you can even shorten that to just:
    @
    #include <QApplication>
    #include <QTimer>

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

    QTimer::singleShot(5000, [&](){
        //do stuff
        a.exit();
    });
    
    return a.exec&#40;&#41;;
    

    }@



  • Even shorter: (couldn't resist nagging):
    @
    ...
    QTimer::singleShot(5000, [&]{
    ...
    @

    You don't need any parenthesis when lambda has no arguments, "learned that":http://qt-project.org/forums/viewreply/216748/ from JKSH the other day :-)



  • Hello Chris,

    that's what i was looking for, but i have still problems with it.

    I'm on :
    Qt Creator 3.3.0 (opensource)
    Based on Qt 5.4.0 (GCC 4.6.1, 32 bit)

    @//CK// #include <QApplication>
    #include <QCoreApplication>
    #include <QTimer>
    #include <QDebug>

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

    //CK// QApplication a(argc, argv);
    QCoreApplication a(argc, argv);

    QTimer::singleShot(5000, [&](){
        //do stuff
    
        qDebug() << "bingo";
        a.exit();
    });
    
    return a.exec&#40;&#41;;
    

    }
    @

    erro message:
    @.../main.cpp:25: error: no matching function for call to 'QTimer::singleShot(int, main(int, char**)::__lambda0)'
    });
    ^@


  • Moderators

    @wally123: As I said that's a Qt 5.4 feature. Which Qt version are you using (not which version Qt Creator was build with) and which compiler? If you're not sure type QT_VERSION_STR in your code and press F2 to find out.

    For gcc you need to add @CONFIG += c++11@ in your .pro file to have lambdas (but the error mentions lambda so you probably have that already).

    @hskoglund: nice hint! on to the search/replace in my codebase... :)



  • Chris,

    bq. not which version Qt Creator was build with

    sure , sorry
    @
    #define QT_VERSION_STR "5.4.0"@

    Options/ Buile&Run: Kits, QT-versions reporting also 5.4.0
    Yes gcc and adding to .pro file
    @CONFIG += c++11@

    made it work. Also thanks to hskoglund
    Even it's a short code, it contains some interesting new stuff for me
    to learn more about. especially this part:

    @ &{ .... };@

    thx wally



  • The following is what i primarly intended to ask for :)

    @#include <QCoreApplication>
    #include <QTimer>
    #include <QDebug>

    void timer1_expired() {
    qDebug() << "timer1 expired";
    }

    void timer2_expired() {
    qDebug() << "timer2 expired";
    }

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

    QTimer::singleShot(4000, timer1_expired);
    QTimer::singleShot(8000, timer2_expired);
    
    qDebug() << "main";
    
    return a.exec&#40;&#41;;
    

    }@


  • Moderators

    [quote author="hskoglund" date="1423561353"]You don't need any parenthesis when lambda has no arguments, "learned that":http://qt-project.org/forums/viewreply/216748/ from JKSH the other day :-)[/quote]Pay it forward! ;-)



  • not sure it makes sense to add to an already solved topic (?)

    in above lecture the main reason i did not solve the problem myself
    by trial & error was Chris' hint to use
    @CONFIG += c++11@

    in .pro file.
    It's mentioned here "docs":http://doc.qt.io/qt-5/qmake-variable-reference.html#config
    but this is not really an explanation.
    Where can i find an explanation what this instruction really effects
    and in which cases to use ?


  • Moderators

    [quote author="wally123" date="1423669903"]not sure it makes sense to add to an already solved topic (?)[/quote]That's perfectly ok if you are continuing to discuss the same topic.

    [quote author="wally123" date="1423669903"]in above lecture the main reason i did not solve the problem myself
    by trial & error was Chris' hint to use
    @CONFIG += c++11@

    in .pro file.
    It's mentioned here "docs":http://doc.qt.io/qt-5/qmake-variable-reference.html#config
    but this is not really an explanation.
    Where can i find an explanation what this instruction really effects
    and in which cases to use ?
    [/quote]That instructions tells your compiler to enable "C++11":http://www.cprogramming.com/c++11/what-is-c++0x.html features.

    The QTimer code that Chris posted uses a lambda expression, which was introduced in C++11; that's why you need that command.



  • thx
    needless to say which expression is now in center of interest :)
    λ


  • Moderators

    [quote author="wally123" date="1423671202"]thx
    needless to say which expression is now in center of interest :)
    λ [/quote]You're welcome! It's a very nice toy indeed. Once you learn how to use it, I'm sure you'll be using it lots. ;)

    Lambda expressions make it much easier to write event-driven code in Qt. If your slot is only used in one place, you can define it as a lambda right in the "connect" statement. No need to create a separate function in your class anymore. See "Making Connections to Lambda Expressions":http://doc.qt.io/qt-5/signalsandslots-syntaxes.html#making-connections-to-lambda-expressions.

    Happy coding!


  • Moderators

    The doc pretty much says everything there is to say about it but I'll try to expand.

    As you may or may not know C++ goes through revisions and different compilers have varying support for the latest standard.
    In case of GCC the default is c++03 and to enable the latest c++11 you need a compiler param -std=c++11. To make this portable across compilers qmake exposes this as the above CONFIG switch and does the right thing for the given compiler to enable c++11 support.

    Lambdas (or captures or anonymous functions as they are also known) are new part of c++11 standard.
    "Here":https://gcc.gnu.org/projects/cxx0x.html is a comprehensive table listing all the new features of c++11 and which gcc version supports what.

    Oh, the next revision of c++ has been already voted in last year so expect something like CONFIG+=c++14 available pretty soon.

    As for which c++11/14 features are supported/used by Qt you can travel the net, e.g. "here":http://woboq.com/blog/cpp11-in-qt5.html or "here":http://woboq.com/blog/cpp14-in-qt.html



  • This forum is really incredible efficient for learning !

    btw.: does anybody pay you for your time and sharing knowhow ?


  • Moderators

    Don't know about others but I pretty much operate on a "beerware":http://en.wikipedia.org/wiki/Beerware based rules.


  • Moderators

    [quote author="wally123" date="1423672274"]This forum is really incredible efficient for learning ![/quote]It is. :) We hope that one day, when you're a Qt veteran, you'll come back and help newcomers too!

    [quote author="wally123" date="1423672274"]btw.: does anybody pay you for your time and sharing knowhow ?[/quote]I suppose I get paid in pats-on-the-back, satisfaction, reputation, and knowledge + experience.

    I often learn new things while doing research to answer a question. In turn, that helps me produce better code, which makes my customers happy, so they keep paying me.

    BTW, it's not limited to this forum; the hacker community is full of volunteers, e.g. "StackOverflow":http://stackoverflow.com/



  • :)


Log in to reply
 

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