Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved QTimer::singleShot - forward parameter to SLOT called

    General and Desktop
    qtimer
    4
    16
    21686
    Loading More Posts
    • 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.
    • McLion
      McLion last edited by

      Hi

      I'd like to do something like this:

      QTimer::singleShot(5000, this, SLOT(MySlot(iID)));
      

      to get the ID (can be 1 to 16) and know which ID did kick off the singleShot.
      However, that seems not to be possible with on-board tools.

      Any idea how this can be easily implemented?
      Thanks
      McL

      1 Reply Last reply Reply Quote 0
      • m.sue
        m.sue last edited by m.sue

        Hi,
        how about

        QTimer::singleShot(5000, this, [] () {MySlot(0); });
        QTimer::singleShot(5000, this, [] () {MySlot(1); });
        

        etc. or

        for (iId=0; iId < 2; ++iId)
          QTimer::singleShot(5000, this, [iId] () {MySlot(iId); });
        

        if you want to keep the variable.
        -Michael.

        McLion 1 Reply Last reply Reply Quote 2
        • McLion
          McLion @m.sue last edited by McLion

          @m.sue said in QTimer::singleShot - forward parameter to SLOT called:

          [] {MySlot(0); }

          Hhmmm ..
          Is this documented somewhere: ... [] {MySlot(0); }.. frankly, I have no clue and the compiler doesn't like it either.

          m.sue 1 Reply Last reply Reply Quote 0
          • m.sue
            m.sue @McLion last edited by m.sue

            @McLion: You are right. I forgot the (): [] () {MySlot(0); }
            It's the lambda function of C++11.

            McLion 1 Reply Last reply Reply Quote 0
            • McLion
              McLion @m.sue last edited by

              @m.sue
              Quickly added CONFIG += c++11 ... still no joy.
              I get expected primary-expression before '[' token

              1 Reply Last reply Reply Quote 0
              • VRonin
                VRonin last edited by VRonin

                Ancient compiler and/or Qt version I guess. You can use QSignalMapper then:

                QSignalMapper* idMapper= new QSignalMapper(this)
                connect(idMapper,SIGNAL(mapped(int)),this,SLOT(MySlot(int)));
                for (int iId=0; iId < 2; ++iId){
                QTimer* tempTimer=new tempTimer(this);
                tempTimer->setInterval(5000);
                tempTimer->setSingleShot(true);
                idMapper->setMapping(tempTimer, iId);
                connect(tempTimer,SIGNAL(timeout()),idMapper,SLOT(map()));
                connect(tempTimer,SIGNAL(timeout()),tempTimer,SLOT(deleteLater()));
                tempTimer->start();
                }
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                McLion 1 Reply Last reply Reply Quote 3
                • McLion
                  McLion @VRonin last edited by

                  @VRonin
                  Thanks!
                  any idea why it builds ok but crashes on this line?
                  iGUIidMapper->setMapping(tempTimer, iGUIid);

                  VRonin 1 Reply Last reply Reply Quote 0
                  • VRonin
                    VRonin @McLion last edited by

                    @McLion In all probability because either tempTimer or iGUIidMapper are rouge pointers. Can you show us the rest of the code?

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    McLion 1 Reply Last reply Reply Quote 0
                    • McLion
                      McLion @VRonin last edited by

                      @VRonin
                      in constructor:

                      QSignalMapper* iGUIidMapper = new QSignalMapper(this);
                      connect(iGUIidMapper, SIGNAL(mapped(int)), this, SLOT(HandleGUIloadTimeOut(int)));
                      

                      .. and in function ..

                      void QTGUI_MainWindow::HandleGUIloadTimeOut(int iGUIid)
                      {
                        static uint iIsRunning = 0;
                      
                        if(iIsRunning & (0x01 << iGUIid))
                        { iIsRunning &= ~(0x01 << iGUIid);
                          webGUI_loadFinished(false);
                        }
                        else
                        { iIsRunning |= (0x01 << iGUIid);
                          QTimer* tempTimer = new QTimer(this);
                          tempTimer->setInterval(5000);
                          tempTimer->setSingleShot(true);
                          GUIidMapper->setMapping(tempTimer, iGUIid);
                          connect(tempTimer, SIGNAL(timeout()), GUIidMapper, SLOT(map()));
                          connect(tempTimer, SIGNAL(timeout()), tempTimer, SLOT(deleteLater()));
                          tempTimer->start();
                        }
                      }
                      
                      1 Reply Last reply Reply Quote 0
                      • m.sue
                        m.sue last edited by m.sue

                        You use QSignalMapper* iGUIidMapper in the contructor, so the variable iGUIidMapper is unknown outside of the contructor.

                        McLion 1 Reply Last reply Reply Quote 1
                        • McLion
                          McLion @m.sue last edited by

                          @m.sue
                          .. and in *.h
                          QSignalMapper* iGUIidMapper;
                          That should do it.

                          m.sue 1 Reply Last reply Reply Quote 0
                          • m.sue
                            m.sue @McLion last edited by m.sue

                            @McLion: In the constructor you set the variable (locally) declared in the contructor, but the member variable declared in *.h is still unset.

                            McLion 1 Reply Last reply Reply Quote 1
                            • McLion
                              McLion @m.sue last edited by

                              @m.sue
                              Jeeezz... am I blind ?!?
                              Fixed .. I'll post back once it fully works.
                              Thanks!

                              mrjj 1 Reply Last reply Reply Quote 1
                              • mrjj
                                mrjj Lifetime Qt Champion @McLion last edited by

                                @McLion
                                It's a classic :)
                                Copy def from .h, add the new part. Forget to remove the type :)

                                1 Reply Last reply Reply Quote 0
                                • VRonin
                                  VRonin last edited by

                                  btw I don't think I like what you are doing with iIsRunning I think it shouldn't be static (different windows should use different iIsRunning) and it's unnecessarily limited (if iGUIid >31 you have a problem). a QSet<int> to store the iGUIid is much more flexible

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  McLion 1 Reply Last reply Reply Quote 4
                                  • McLion
                                    McLion @VRonin last edited by McLion

                                    @VRonin
                                    Yep - you're absolutely right. It's unfinished code and currently a draft to start off with a ID range of 1 as a proof of concept.
                                    Will take you input into account - Thanks a lot!

                                    1 Reply Last reply Reply Quote 0
                                    • First post
                                      Last post