Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Call a function every time scrollbar change value
jk_mk last edited by
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?
loladiro last edited by
You can use the "sliderMoved":http://doc.qt.nokia.com/latest/qabstractslider.html#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
// 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.
loladiro last edited by
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!
giesbert last edited by
closed due to duplicate of "this thread":http://developer.qt.nokia.com/forums/viewthread/6380/