QTimer::singleShot - forward parameter to SLOT called

  • 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?

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

  • @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.

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

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

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

    QSignalMapper* idMapper= new QSignalMapper(this)
    for (int iId=0; iId < 2; ++iId){
    QTimer* tempTimer=new tempTimer(this);
    idMapper->setMapping(tempTimer, iId);

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

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

  • @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);
      { iIsRunning |= (0x01 << iGUIid);
        QTimer* tempTimer = new QTimer(this);
        GUIidMapper->setMapping(tempTimer, iGUIid);
        connect(tempTimer, SIGNAL(timeout()), GUIidMapper, SLOT(map()));
        connect(tempTimer, SIGNAL(timeout()), tempTimer, SLOT(deleteLater()));

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

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

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

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

  • Qt Champions 2017

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

  • 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

  • @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!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.