Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

I want to synchronize the slider bar with my mouse wheel?



  • I have a grid view and showing the multiple images using the slider bar top to bottom. I have a query when I slider the bar, an image will change inside the grid, after a middle of the slider, I try to scroll the mouse wheel. The image will be changing from the first image. I need to synchronize the mouse wheel and slider bar. Once the mouse wheel rotates respective to the slider bar also move. Also, I need a cyclic slider bar.

    I am attaching the image view :

    0_1532613919831_slider.png

    Thanks for your help
    Amar



  • Qt provides a wheel event that you can use. Simply calculate the amount to change the slider according to the delta value. It would also possibly be better to look at alternative implementations which could provide a scroll area for you.



  • @amarism
    I presume that "Slider" you show is a QSlider? From what I have seen I would expect that to correctly scroll for mousewheel as well as mouse-slider-drag. You suggest they are not "integrated", in that doing a mousewheel re-starts from the top ignoring where the user has already mouse-dragged the slider too, is that right? You might need to show the code being used, because my thought is that it ought be working the way you want already.

    You might alternatively see if a QScrollBar works better for you (it should certainly handle mouse drag + mousewheel together) if you are not wedded to a QSlider. As @ArsenArsen said, you might want instead to change code over to using something like a QScrollArea to handle the "scrolling" for you.



  • @JonB

    MainWindow.cpp

    connect(ui->Slider, SIGNAL(valueChanged(int)), this, SLOT(SetSlice(int)));

    void QMainWindow::SetSlice(int SliderPos)
    {
    qDebug() << SliderPos;

    if (!mainObject)
    	return;
    
    if(ui->Slider->hasFocus())
    	mainObject->ImageSlicing(ui->Slider->sliderPosition(), 0);
    

    }

    viewer.cpp

    void Viewer::ImageSlicing(int QtSliderPos, int view)
    {
    if (view > 2)
    return;
    int CurrSlice = ResliceViewer[view]->GetSlice();

    	ResliceViewer[view]->SetSlice(QtSliderPos);
    	ResliceViewer[view]->Modified();
    

    }

    QPair<int, int> Viewer::GetSetSliceRange(QSlider *QSliderWidget, int view)
    {
    int SliceMax = ResliceViewer[view]->GetSliceMax();
    int SliceMin = ResliceViewer[view]->GetSliceMin();

    if (!QSliderWidget)
    	return qMakePair(m_SliceMax, m_SliceMin);
    
    QSliderWidget->setMinimum(m_SliceMin);
    QSliderWidget->setMaximum(m_SliceMax);
    
    return qMakePair(m_SliceMax , m_SliceMin);
    

    }



  • @ArsenArsen How to make slider as a cyclic?


Log in to reply