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. QTimer::singleShot - forward parameter to SLOT called
Forum Updated to NodeBB v4.3 + New Features

QTimer::singleShot - forward parameter to SLOT called

Scheduled Pinned Locked Moved Solved General and Desktop
qtimer
16 Posts 4 Posters 25.1k Views 3 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.
  • McLionM Offline
    McLionM Offline
    McLion
    wrote on last edited by
    #1

    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
    0
    • m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by m.sue
      #2

      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.

      McLionM 1 Reply Last reply
      2
      • m.sueM 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.

        McLionM Offline
        McLionM Offline
        McLion
        wrote on last edited by McLion
        #3

        @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.sueM 1 Reply Last reply
        0
        • McLionM 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.sueM Offline
          m.sueM Offline
          m.sue
          wrote on last edited by m.sue
          #4

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

          McLionM 1 Reply Last reply
          0
          • m.sueM m.sue

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

            McLionM Offline
            McLionM Offline
            McLion
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              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

              McLionM 1 Reply Last reply
              3
              • VRoninV 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();
                }
                
                McLionM Offline
                McLionM Offline
                McLion
                wrote on last edited by
                #7

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

                VRoninV 1 Reply Last reply
                0
                • McLionM McLion

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

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @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

                  McLionM 1 Reply Last reply
                  0
                  • VRoninV VRonin

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

                    McLionM Offline
                    McLionM Offline
                    McLion
                    wrote on last edited by
                    #9

                    @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
                    0
                    • m.sueM Offline
                      m.sueM Offline
                      m.sue
                      wrote on last edited by m.sue
                      #10

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

                      McLionM 1 Reply Last reply
                      1
                      • m.sueM m.sue

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

                        McLionM Offline
                        McLionM Offline
                        McLion
                        wrote on last edited by
                        #11

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

                        m.sueM 1 Reply Last reply
                        0
                        • McLionM McLion

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

                          m.sueM Offline
                          m.sueM Offline
                          m.sue
                          wrote on last edited by m.sue
                          #12

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

                          McLionM 1 Reply Last reply
                          1
                          • m.sueM 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.

                            McLionM Offline
                            McLionM Offline
                            McLion
                            wrote on last edited by
                            #13

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

                            mrjjM 1 Reply Last reply
                            1
                            • McLionM McLion

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

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

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

                              1 Reply Last reply
                              0
                              • VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by
                                #15

                                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

                                McLionM 1 Reply Last reply
                                4
                                • VRoninV VRonin

                                  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

                                  McLionM Offline
                                  McLionM Offline
                                  McLion
                                  wrote on last edited by McLion
                                  #16

                                  @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
                                  0

                                  • Login

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