Bring slider to mouse click position.
-
Hi,
I have a QWidget application. I open a window which will play the selected video. I have a slider on that window which moves one step at a time. I want to move the slider to the position where the user clicks the left mouse button. I have written the below logic:
Qt::MouseButtons btns = QApplication::mouseButtons(); QPoint localMousePos = slider->mapFromGlobal(QCursor::pos()); bool clickOnSlider = (btns & Qt::LeftButton) && (localMousePos.x() >= 0 && localMousePos.y() >= 0 && localMousePos.x() < slider->size().width() && localMousePos.y() < slider->size().height()); if (clickOnSlider) { float posRatio = localMousePos.x() / (float )slider->size().width(); int sliderRange = slider->maximum() - slider->minimum(); int sliderPosUnderMouse = slider->minimum() + sliderRange * posRatio; if (sliderPosUnderMouse != newPos) { slider->setValue(sliderPosUnderMouse); return; } }
Problem with this code is that the slider moves to the mouse click position but then immediately comes back to the position of the video playing.
Any help would be appreciated.
-
Hi,
I have a QWidget application. I open a window which will play the selected video. I have a slider on that window which moves one step at a time. I want to move the slider to the position where the user clicks the left mouse button. I have written the below logic:
Qt::MouseButtons btns = QApplication::mouseButtons(); QPoint localMousePos = slider->mapFromGlobal(QCursor::pos()); bool clickOnSlider = (btns & Qt::LeftButton) && (localMousePos.x() >= 0 && localMousePos.y() >= 0 && localMousePos.x() < slider->size().width() && localMousePos.y() < slider->size().height()); if (clickOnSlider) { float posRatio = localMousePos.x() / (float )slider->size().width(); int sliderRange = slider->maximum() - slider->minimum(); int sliderPosUnderMouse = slider->minimum() + sliderRange * posRatio; if (sliderPosUnderMouse != newPos) { slider->setValue(sliderPosUnderMouse); return; } }
Problem with this code is that the slider moves to the mouse click position but then immediately comes back to the position of the video playing.
Any help would be appreciated.
@mbatra said in Bring slider to mouse click position.:
Problem with this code is that the slider moves to the mouse click position but then immediately comes back to the position of the video playing.
How have you linked the slider to the position of the video playing. It sounds like it shows the movie position but does not update the position on the video when you change the slider position, so that it will jump to wherever it was after you have moved it. Is that the case? Moving the slider must be set to affect the video position, not just the other way round.
Additionally, I don't know whether you need to implement your own click-on-slider logic as you have done, doesn't it accept a click to a new position just as much as it would accept a drag?
-
@mbatra said in Bring slider to mouse click position.:
Problem with this code is that the slider moves to the mouse click position but then immediately comes back to the position of the video playing.
How have you linked the slider to the position of the video playing. It sounds like it shows the movie position but does not update the position on the video when you change the slider position, so that it will jump to wherever it was after you have moved it. Is that the case? Moving the slider must be set to affect the video position, not just the other way round.
Additionally, I don't know whether you need to implement your own click-on-slider logic as you have done, doesn't it accept a click to a new position just as much as it would accept a drag?
-
-
Hi,
I just solved the same problem using a normal QSlider and its "actionTriggered(int action)" signal. There are the actions "SliderPageStepAdd" and "SliderPageStepSub" that are triggered when I click on the track.void MainWindow::on_horizontalSliderSeek_actionTriggered(int action) { if(action == QAbstractSlider::SliderPageStepAdd || action == QAbstractSlider::SliderPageStepSub){ QPoint Mouse = ui->horizontalSliderSeek->mapFromGlobal(QCursor::pos()); QSize Size = ui->horizontalSliderSeek->size(); int max = ui->horizontalSliderSeek->maximum(); int position = (Mouse.rx()*max)/qMax(Size.width(),1); ui->horizontalSliderSeek->setValue(position); QMplayer.setPosition((QMplayer.duration()*position+(max/2))/max); } } void MainWindow::on_horizontalSliderSeek_sliderMoved(int position) { int max = ui->horizontalSliderSeek->maximum(); QMplayer.setPosition((QMplayer.duration()*position+(max/2))/max); }
I use the "sliderMoved" signal to handle movement using the knob. I also tried checking for the "SliderMove" action in the "actionTriggered(int action)" signal. But somehow the "sliderMoved" signal is triggered more often and seeking in the movie this way just feels a little more smooth.
The "qMax(Size.width(),1)" is just to prevent a division by 0 if for some reason the Size.width returns 0.