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 need stop without signal
QtWS25 Last Chance

[SOLVED] QTimer need stop without signal

Scheduled Pinned Locked Moved General and Desktop
13 Posts 6 Posters 12.1k Views
  • 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.
  • G Offline
    G Offline
    Gourmand
    wrote on 12 Jun 2012, 16:15 last edited by
    #1

    If is start QTimer like this:

    @timer.singleShot( 5000, this, SLOT(loadimg()) );@

    then if I need stop timer but not to call slot on stop - should this work?

    @disconnect( this, SLOT(loadimg()) );
    timer.stop();@

    Qt4.7 used

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on 12 Jun 2012, 17:41 last edited by
      #2

      Of course, it doesn't work, as the timer never started ;-)
      [quote author="Gourmand" date="1339517710"]
      then if I need stop timer but not to call slot on stop - should this work?

      @
      timer.stop();
      @

      Qt4.7 used[/quote]

      Note that, singleShot is a static member of class QTimer, it has no-relationship with the object timer.

      Seem what you need is

      @
      QTimer::setSingleShot()
      @

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Gourmand
        wrote on 13 Jun 2012, 05:56 last edited by
        #3

        Yes I didn't notice that QTimer is NOT singleShot by default. The name of function QTimer::singleShot() confused me.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          miroslav
          wrote on 14 Jun 2012, 06:09 last edited by
          #4

          Actually, a timer did get started. Calling
          @
          timer.singleShot(...)
          @

          will call the static function singleShot(), and after 5000 msecs, the signal will be sent. But it won't be your timer object that sends it, but an anonymous timer created by singleShot(). This is why calling stop() on the timer object does not work. You need to set the properties on timer (singleshot, the timeout), connect to it's timeout signal, and then call start() on timer.

          Mirko Boehm | mirko@kde.org | KDE e.V.
          FSFE Fellow
          Qt Certified Specialist

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jeroentjehome
            wrote on 14 Jun 2012, 06:58 last edited by
            #5

            Yup, so this needs to be done:
            @
            timer.setSingleshot(true);
            connect(timer, SIGNAL(timeout()), this, SLOT(loading()));
            timer->start(5000);
            @ Have fun ;-)

            Greetz, Jeroen

            1 Reply Last reply
            0
            • G Offline
              G Offline
              Gourmand
              wrote on 14 Jun 2012, 07:19 last edited by
              #6

              [quote author="miroslav" date="1339654187"]Actually, a timer did get started. Calling
              @
              timer.singleShot(...)
              @

              will call the static function singleShot(), and after 5000 msecs, the signal will be sent. But it won't be your timer object that sends it, but an anonymous timer created by singleShot(). This is why calling stop() on the timer object does not work. You need to set the properties on timer (singleshot, the timeout), connect to it's timeout signal, and then call start() on timer. [/quote]

              Of course this explains strange behavior of timer after stop()! But PLEASE point me to documentation where it is clearly written that after timer::singleShot() actually ANOTHER timer sends signal after timeout and stop does not actually work. PLEASE! If this is written - than I just did not read carefully. If it is NOT written - then this is just another one fault in Qt documentation.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                Gourmand
                wrote on 14 Jun 2012, 07:21 last edited by
                #7

                Jeroentje - thnx but after miroslav's hint I know myself how to solve my problem.

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on 14 Jun 2012, 07:49 last edited by
                  #8

                  [quote author="Gourmand" date="1339658356"]Of course this explains strange behavior of timer after stop()! But PLEASE point me to documentation where it is clearly written that after timer::singleShot() actually ANOTHER timer sends signal after timeout and stop does not actually work. PLEASE! If this is written - than I just did not read carefully. If it is NOT written - then this is just another one fault in Qt documentation. [/quote]

                  The actual problem is that you are calling a "static":http://qt-project.org/doc/qt-4.8/qtimer.html#static-public-members member method (which are indicated by <code>[static]</code> in the "documentation":http://qt-project.org/doc/qt-4.8/qtimer.html#singleShot) on an object, and a static member has no access to the object is has been called on (no this).

                  Although using the dot operator <code>timer.singleShot(...)</code> is valid C++, it is quite misleading, as you have to be aware of that your are not calling a member function, but rather a static member function. Using the scope resolution operator <code>QTimer::singleShot(...)</code> usually illustrates this.

                  If you've found a fault in the Qt documentation (although it technically isn't in this case), feel free to file a "bug report":https://bugreports.qt-project.org/secure/Dashboard.jspa. Suggestions for improvements, possibly in the form of a "doc note":http://qt-project.org/blog/view/doc-notes-we-have-a-vision are welcome as well.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    Gourmand
                    wrote on 14 Jun 2012, 08:02 last edited by
                    #9

                    [quote author="Lukas Geyer" date="1339660191"]
                    The actual problem is that you are calling a "static":http://qt-project.org/doc/qt-4.8/qtimer.html#static-public-members member method (which are indicated by <code>[static]</code> in the "documentation":http://qt-project.org/doc/qt-4.8/qtimer.html#singleShot) on an object, and a static member has no access to the object is has been called on (no this).
                    [/quote]

                    But the QTimer is incapsulated and I do not know how it works inside and which data are not accessible by static singleShot(). I can assume that singleShot() calls some non-static private members to make this timer possible to stop(). I just read documentation how to use classes - but I do not analyze how they work in source code... This is C++ with incapsulation, isn't it?

                    After tens of years od software development I know this truth: if something can mislead then this must be explained in docs quite clear.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      veeraps
                      wrote on 14 Jun 2012, 08:42 last edited by
                      #10

                      If you look into Qt Open Source Code, QTimer::singleShot() creates another timer instance of QSingleShotTimer which is internal to the file. Basically this QSingleShotTimer is derived from QObject and it connects the signal and slot and also starts the timer thru QObject::startTimer().

                      timerEvent() is also overridden inside this QSingleShotTimer, so that once timer is timed out, timer created (inside QSingleShotTimer) will be deleted.

                      Having said that QTimer::singleShot() will create its own timer and will not access any members of the object that is called on as you have done i.e., timer.singleShot() won't access any timer members.

                      By going thru the document also,

                      @void QTimer::singleShot ( int msec, QObject * receiver, const char * member ) [static]@

                      This static function calls a slot after a given time interval.

                      It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

                      It says that singleShot() static member is convenient one as a local QTimer object need not be created.

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        veeraps
                        wrote on 14 Jun 2012, 08:43 last edited by
                        #11

                        Also one suggestion - if this topic is concluded with result, appreciate the originator to prefix the subject/topic with [SOLVED], so as to know that the answer is known.

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          lgeyer
                          wrote on 14 Jun 2012, 08:54 last edited by
                          #12

                          [quote author="Gourmand" date="1339660973"]I do not know how it works inside and which data are not accessible by static singleShot(). I can assume that singleShot() calls some non-static private members to make this timer possible to stop().[/quote]
                          No. Be aware that in C++ a static member function does not have access to the calling object and its non-static members and non-static member functions, as both require an implicit this pointer (which is non-existent for static member functions, as long as you do not provide it explicitly).

                          If QTimer::singleShot() would depend on (or modify) the calling object, it would not be static.

                          [quote author="Gourmand" date="1339660973"]if something can mislead then this must be explained in docs quite clear.[/quote]You are right. I don't think it is misleading in this case, because of the member beeing static and no explicit reference to an object beeing passed it is quite clear that it will not (or can not) depend on the calling object.

                          But as said, feel free to file a suggestion to the bug tracker or add a doc note on your own.

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            Jeroentjehome
                            wrote on 14 Jun 2012, 10:49 last edited by
                            #13

                            Well, it looks pretty clear to me and when reading the documentation carefully with a clear C++ education not misleading at all. Still, feel free to make a suggestion to the bug tracker. Maybe they will update the documentation.

                            Greetz, Jeroen

                            1 Reply Last reply
                            0

                            3/13

                            13 Jun 2012, 05:56

                            topic:navigator.unread, 10
                            • Login

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