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. Beginner SIGNALS and SLOTS question
Qt 6.11 is out! See what's new in the release blog

Beginner SIGNALS and SLOTS question

Scheduled Pinned Locked Moved General and Desktop
16 Posts 5 Posters 9.1k 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.
  • G Offline
    G Offline
    gobnat
    wrote on last edited by
    #1

    Probably very silly question but I cannot figure it out.

    In a window class I have this:

    @//Create, connect, and start the animation timer
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()),animationClass,SLOT(animate()));
    timer->start(1000);@

    and in animationCLass I have this:
    @
    void animationClass::animate(){

    //Various things from several other classes are animated here
    }@

    When certain events take place within the animate() function I want the whole animation to stop.

    How do I do that?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Santosh Reddy
      wrote on last edited by
      #2

      What is animationClass?
      If is derived from QAbstractAnimation then use
      @QAbstractAnimation::stop () [slot]@

      SS

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on last edited by
        #3

        Does it mean you want to stop the timer, when that event has occurred?

        Is your Animation class in control of the time? If so, simply let it call QTimer::stop() then.

        If the QTimer is "outside" the animation class and the animation class has no way to stop it directly, let your animation class a signal, like, stopConditionEncountered(). Then, whichever class/object is in control of the timer can react on that signal - and stop the timer.

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gobnat
          wrote on last edited by
          #4

          AnimationClass is a QWidget

          @class Screen : public QWidget@

          The connect function is in a Window class which takes the a Screen object as a parameter

          @Window w(screen);@

          I tried calling stop() from inside the animate() function but I have no access to the timer there.
          I also tried disconnect() but it didn't seem to do anything.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on last edited by
            #5

            Then I'd suggest to emit a signal, as suggested above.

            Either that, or consider moving the QTimer into your Animation class/object.

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • U Offline
              U Offline
              utcenter
              wrote on last edited by
              #6

              This is much easier in QML, if you bind your animation to a bool value, it will interrupt as soon as it is false.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gobnat
                wrote on last edited by
                #7

                bq. If the QTimer is “outside” the animation class and the animation class has no way to stop it directly, let your animation class a signal, like, stopConditionEncountered(). Then, whichever class/object is in control of the timer can react on that signal – and stop the timer.

                Ok that would be awesome but I have absolutely no idea how to do that. I there somewhere that explains how to do something like that?

                DO I have to set up a whole new SIGNAL and SLOT connection heading the other way (from the widget to the window) os can I just signal the connect to stop from inside the widget?

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gobnat
                  wrote on last edited by
                  #8

                  bq. This is much easier in QML, if you bind your animation to a bool value, it will interrupt as soon as it is false.

                  Unfortunately I am stuck with someone else's implementation and I cannot change it very much.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MuldeR
                    wrote on last edited by
                    #9
                    1. You add a signal to your Animation class
                    2. You add a corresponding slot to the class that has the QTimer
                    3. You connect that signal to the slot (and yes, it's a separate connection)
                    4. You make Animation class/object emit the signal at the right time
                    5. You make the other class/object stop the timer in the slot function

                    --

                    See also:
                    http://qt-project.org/doc/qt-4.8/signalsandslots.html

                    My OpenSource software at: http://muldersoft.com/

                    Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                    Go visit the coop: http://youtu.be/Jay...

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      MuldeR
                      wrote on last edited by
                      #10

                      Alternative approach:

                      In the constructor of the Animation class, you pass a pointer to your QTimer, so the Animation class can connect itself to that timer. This has the advantage that the Animation class can also store that pointer to the QTimer in a member variable for stopping the QTimer later (directly!).

                      Though this approach has some drawbacks too: If the QTimer gets destroyed "outside" of Animation class you have a dangling pointer. Also, when Animation class decides to stop the QTimer, other classes listening to that same timer are effected too. And they have no chance to prevent Animation class from stopping the timer.

                      My OpenSource software at: http://muldersoft.com/

                      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                      Go visit the coop: http://youtu.be/Jay...

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        pgroen
                        wrote on last edited by
                        #11

                        Create the QTimer as a SingleShot. After the timeout() it will stop, until you start it again. Create a signal in the Widget which will be fired as soon as the animation is done. Use that signal to start the timer again.

                        Peter M. Groen
                        Open Systems Development

                        'Will Killing Time Affect The Future?'
                        'I think It Will, In The End'

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          gobnat
                          wrote on last edited by
                          #12

                          I am a little confused here.

                          In either case you suggest MuldeR I need at least a reference to the class I want to signal and/or get a pointer to it's timer don't I?

                          Since the Window is already taking the Screen:QWidget as a parameter, I can't pass a reference to the Window - or any of it's member variables - as they don't exist yet.

                          I do like the second Idea, just not sure how to pull it off.

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            MuldeR
                            wrote on last edited by
                            #13

                            If you connect signals/slots of two objects, you always need references to these objects - at that point. Though, the objects that get connected don't need to know of each other. In the first approach I suggested, Animation class doesn't need a reference to QTimer. It doesn't even know that QTimer exists. It just emits a timeToStopNow() signal when it wants to stop and that's it.

                            My OpenSource software at: http://muldersoft.com/

                            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                            Go visit the coop: http://youtu.be/Jay...

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              gobnat
                              wrote on last edited by
                              #14

                              bq. In the constructor of the Animation class, you pass a pointer to your QTimer, so the Animation class can connect itself to that timer. This has the advantage that the Animation class can also store that pointer to the QTimer in a member variable for stopping the QTimer later (directly!).
                              Though this approach has some drawbacks too: If the QTimer gets destroyed “outside” of Animation class you have a dangling pointer. Also, when Animation class decides to stop the QTimer, other classes listening to that same timer are effected too. And they have no chance to prevent Animation class from stopping the timer.

                              Wow ok I am an idiot, I just had to pass the timer as a parameter to the Window and the Widget ><

                              Then I could call stop() as your alternative method suggested.

                              I don't think it's ideal, but I have no others classes listening to the SIGNAL.

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                MuldeR
                                wrote on last edited by
                                #15

                                Example:

                                @OuterClass::foo()
                                {
                                timer = new QTimer(this);
                                connect(timer, SIGNAL(timeout()),animationClass,SLOT(animate()));
                                connect(animationClass, SIGNAL(timeToStopNow()),this,SLOT(bar()));
                                timer->start(1000);
                                }

                                OuterClass::bar()
                                {
                                timer->stop();
                                }@

                                Assuming "timer" is a member variable of OuterClass.

                                My OpenSource software at: http://muldersoft.com/

                                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                                Go visit the coop: http://youtu.be/Jay...

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  gobnat
                                  wrote on last edited by
                                  #16

                                  Ah I see, I misunderstood the documentation about signals.

                                  The help has all been excellent by the way, thank you.

                                  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