Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Bring slider to mouse click position.
Forum Updated to NodeBB v4.3 + New Features

Bring slider to mouse click position.

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mbatra
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • M mbatra

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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?

      M 1 Reply Last reply
      0
      • JonBJ JonB

        @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?

        M Offline
        M Offline
        mbatra
        wrote on last edited by
        #3

        @JonB

        Hi Jon,

        Thank you for the response. I have added the slider change value to player position.
        it worked.

        Thanks.

        1 Reply Last reply
        0
        • M mbatra has marked this topic as solved on
        • P Offline
          P Offline
          ProjektARES
          wrote on last edited by ProjektARES
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved