How can i call a function when the slider stops



  • Hey,
    i have implemented a grid and i have added two QAbstractScrollBar items. One for vertical scrolling and one for horizontal scrolling. I read all th signal, events and action that are related with these classes but i can't find how i "listen" when the slider stop moving. More specfically i want when the slider stops wait for 2 seconds and then call a function.

    ty


  • Lifetime Qt Champion

    Hi,

    AFAIK there isn't.

    What you could do is to have a slot connected to valueChanged(int) and restart a QTimer (connected to the function you want) in there. So once you don't move the slider for a few second, the timer will timeout.

    Hope it helps



  • I want, if possible, to avoid the valueChanged(int) signal because it will be emmited not only when i scoll the slider but when i move the slider for a single step which i don't want.


  • Lifetime Qt Champion

    Then what about a combination of mouseMove and mouseRelease event ?



  • It sounds good but i want the same function to be called when the wheell scrolling stops not just when the slider scrolling stops. Also i would like somenthing like mouseRelease ans mouseNotMove events. Not mouseMove event


  • Lifetime Qt Champion

    Then also look for the wheelEvent.

    You'll have to do the "not moved" detection yourself based on the various input you get.

    Then again, are you sure it's not better to base your unmoved logic on the valueChanged(int) signal ? Don't start the timer if the delta between the last move and this one is a single step.



  • I was thinking something like this:
    @void handleVScrollAction(int action)
    {
    if(action == QAbstractSlider::SliderSingleStepAdd)
    {
    setValueVertical();
    requestSlot();
    }
    else if(action == QAbstractSlider::SliderSingleStepSub)
    {
    setValueVertical();
    requestSlot();
    }
    else if(action == QAbstractSlider::SliderMove)
    {
    drawGrid();
    setValueVertical();
    }
    else if(action == QAbstractSlider::SliderNoAction)
    {
    //start timer
    //when times ends
    //call requestSlot
    }
    }@

    But the QAbstractSlider::SliderNoAction doesn't work as i expexted to. How can i implement your suggetion? When the valueChanged signal is emmited the new value, as far as i know, is instantly stored to the QScrollBar.


  • Moderators

    maybe you are thinking too much straight forward ;)
    Do like SGaist suggested in his first post:
    @
    m_Timer.setSingleShot(true);
    m_Timer.setTimeout(2000);
    connect(&m_Timer, SIGNAL(timeout()), this, SLOT(mySlot()));

    ....

    void handleVScrollAction(int action)
    {
    switch(action)
    {
    case QAbstractSlider::SliderSingleStepAdd:
    case QAbstractSlider::SliderSingleStepSub:
    setValueVertical();
    requestSlot();
    break;
    case QAbstractSlider::SliderMove:
    drawGrid();
    setValueVertical();

            m_Timer.start();   //if the timer is already running it is restarted automatically
        break;
    }
    

    }
    @



  • it worked! Thanks a lot ;)



  • I am opening again because there is one more problem. When i am press the arrow key to scroll the app triggers one of these ations:
    @case QAbstractSlider::SliderSingleStepAdd:
    case QAbstractSlider::SliderSingleStepSub:@
    The first time i press the arrow it does not do anything the second time it moves one step. In addition, when i stop pushing the down arrow key and i want to go up, i press the up arrow key. The first time i press the key it goes down instead of up, the second time it goes up as it should be. Is there any logical explanation for this?


Log in to reply
 

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