Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] QTimer in console application
Forum Updated to NodeBB v4.3 + New Features

[solved] QTimer in console application

Scheduled Pinned Locked Moved General and Desktop
20 Posts 4 Posters 12.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deleted28
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted28
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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;;
            

            }@

            1 Reply Last reply
            0
            • hskoglundH Offline
              hskoglundH Offline
              hskoglund
              wrote on last edited by
              #6

              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 :-)

              1 Reply Last reply
              0
              • D Offline
                D Offline
                deleted28
                wrote on last edited by
                #7

                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)'
                });
                ^@

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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... :)

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    deleted28
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      deleted28
                      wrote on last edited by
                      #10

                      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;;
                      

                      }@

                      1 Reply Last reply
                      0
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #11

                        [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! ;-)

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          deleted28
                          wrote on last edited by
                          #12

                          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 ?

                          1 Reply Last reply
                          0
                          • JKSHJ Offline
                            JKSHJ Offline
                            JKSH
                            Moderators
                            wrote on last edited by
                            #13

                            [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.

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              deleted28
                              wrote on last edited by
                              #14

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

                              1 Reply Last reply
                              0
                              • JKSHJ Offline
                                JKSHJ Offline
                                JKSH
                                Moderators
                                wrote on last edited by
                                #15

                                [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!

                                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                1 Reply Last reply
                                0
                                • Chris KawaC Offline
                                  Chris KawaC Offline
                                  Chris Kawa
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  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

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    deleted28
                                    wrote on last edited by
                                    #17

                                    This forum is really incredible efficient for learning !

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

                                    1 Reply Last reply
                                    0
                                    • Chris KawaC Offline
                                      Chris KawaC Offline
                                      Chris Kawa
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

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

                                      1 Reply Last reply
                                      0
                                      • JKSHJ Offline
                                        JKSHJ Offline
                                        JKSH
                                        Moderators
                                        wrote on last edited by
                                        #19

                                        [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/

                                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          deleted28
                                          wrote on last edited by
                                          #20

                                          :)

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved