How to timeouts a lot of objects



  • Hello,
    I receive a lot of events from a class. This is the slot called:

    @
    void caught(int idx);
    @

    When executed it highlights the row number 'idx' on a QTableView.
    I want to clear each highlight after, say, 250 ms.
    It would be EXTREMELY easy if I can write:

    @
    void caught(int idx)
    {
    model->highlightRow(idx);
    QTimer::singleShot(250, model, SLOT(clearHighlightRow(idx)));
    }
    @

    but for some reasons you cannot call a SLOT with parameters :-(

    What is the recommended way to achieve the same behavior?



  • I found an ugly workaround. In the caught SLOT I enqueue idx and call the singleShot method.
    In its own SLOT I dequeue the idx and clear the highlight.

    It works, but I don't like it at all.



  • If you are using Qt 5.4 and a compiler that supports C++11, you can call QTimer::singleShot() with a lambda function.



  • Unfortunately I'm still with Qt5.3.



  • Works in all of Qt 5.x as long as you have a compiler that support C++/11 or up.

    So, simply write:
    @
    void caught(int idx)
    {
    model->highlightRow(idx);
    QTimer::singleShot(250, model, idx {model->clearHighlight(idx);});
    }
    @

    One thing to take care off: this will cause a crash if model is destructed while there is still a timer running. You can solve that using a QPointer<MyModel> instead of passing a plain pointer to the closure.


Log in to reply
 

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