Please nominate your Qt Champions for 2021!

Call a function every time scrollbar change value

  • Hi,

    What I want to do it may be very easy but I really got confused with the docs and the examples. In my application, I have a QScrollbar and I have created a function. The value of the scrollbar is used as a variable in this function. I just want to run this function every time the value of the Qscrollbar is changed and use the value of the QScrollbar as variable at each time the functions runs. I am using Qt Designer and I have read about custom widgets, but I cannot find any solution to my problem. Could somebody help me with this problem?


  • You can use the "sliderMoved": signal and connect it to you function (as long as it is a slot)

  • This should work..

    Suppose MyClass has the scroll bar.
    // In MyClass.h, declare slot
    void handlerFunction(int);

    // In MyClass.cpp, connect
    connect (<your scroll bar obj>, SIGNAL(valueChanged (int)), this, SLOT(handlerFunction(int)));

    // In MyClass.cpp
    void MyClass::handlerFunction(int val)
    // Handling here

  • @loladiro: From the documentation it seems the valueChanged would be a better choice.


    bq. This signal is emitted when sliderDown is true and the slider moves. This usually happens when the user is dragging the slider.


    bq. This signal is emitted when the slider value has changed, with the new slider value as argument.

  • Depending on what you want to do you can use either signal. The sliderMoved signal is only emitted if the user actually moved it (not if you set it within your application). But as I said it depends on your specific use case.

    EDIT: jim_kaiser: you are really fast

    EDIT2: jim_kaiser You're right, it won't (didn't think about that part). Thanks for the info.

  • Yes, thats true, but will the sliderMoved be emitted when the user doesn't drag the slider, but instead uses the buttons on the ends?

    EDIT: @loladiro: Hehe... well just got here recently.. been coding Qt and solving my problems myself for a few years (Just looooove Qt and what the community has achieved).. and excited to be here.. Its just great to see this community active.. wasn't a member of the QtCentre.. so my first interaction with the Qt community.. :) Not the right place to post this but well wanted to say it somewhere!

  • closed due to duplicate of "this thread":

Log in to reply