3

I m trying to make a media player . The time status of the track is shown using QSlider. Track seek should happen when the user releases the slider somewhere on the QSlider. I had a look on the list of QSlider signals. The one that seems to fit is sliderReleased() which happens when the user releases the slider, but it does not get the latest value of where slider is. So to get sliders latest value I used sliderMoved() and stored reference.

Signal and slot connection

connect(this->ui->songProgress, SIGNAL(sliderMoved(int)), this,
SLOT(searchSliderMoved(int)));
connect(this->ui->songProgress, SIGNAL(sliderReleased()), this,
SLOT(searchSliderReleased()));

Functions

  void MainWindow::searchSliderMoved(int search_percent)
    {
        value=this->ui->songProgress->value();
        std::cout<<"Moved: Slider value"<<value<<std::endl;
    }

Now I am Using the "value" variable inside searchSliderReleased for seeking

void MainWindow::searchSliderReleased()
{
    std::cout<<"Released: Slider value"<<value<<std::endl;

    emit search(value);//seek the track to location value
}

But the problem with above technique is that sliderMoved signal does not give the value where the slider is dropped but it give the location from where the slider was moved. How do I obtain the value where the slider was dropped?

Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126
  • The [documentation](http://qt-project.org/doc/qt-4.8/qabstractslider.html#sliderMoved) for `QSlider` indicates that the `sliderMoved` signal emits the new value of the slider. Try doing `value = search_percent` inside `searchSliderMoved` instead of querying the widget as you're currently doing. – tmpearce Jun 21 '12 at 03:53
  • Yeah I tried it out , But there was this problem that it continuously sends the new value ,even when the user is dragging the slider. – Vihaan Verma Jun 21 '12 at 04:05
  • 9
    Perhaps you want to set `tracking` property to `false` and use `valueChanged` signal (when `tracking==false`, it is only emitted when user drops the slider). – tmpearce Jun 21 '12 at 04:16

4 Answers4

3

You can use the valueChanged(int) signal of the slider, but set the flag

ui->horizontalSlider->setTracking(false);
newandlost
  • 935
  • 2
  • 10
  • 21
2

Just finished building a DiectShow media player with QSlider here at work. Here's a few slot snippets on how I did it.

Widget::sliderPressed()
{
    if( isPlaying() )
    {
        m_wasPlaying = true;
        pause();
    }
    else
    {
        m_wasPlaying = false;
    }

    // Set a flag to denote slider drag is starting.
    m_isSliderPressed = true;
}

Widget::sliderReleased()
{
    if( m_wasPlaying )
    {
        play();
    }

    m_wasPlaying = false;
    m_isSliderPressed = false;
}

Widget::valueChanged( int value )
{
    if( m_isSliderPressed )
    {
         // Do seeking code here
    }     
    else
    {
         // Sometime Qt when the user clicks the slider 
         // (no drag, just a click) Qt will signals
         // pressed, released, valueChanged. 
         // So lets handle that here.
    }   
}
Matthew
  • 2,759
  • 18
  • 28
2

Had the same problem and calling sliderPosition() instead of value() directly when handling sliderReleased() worked for me.

user666412
  • 528
  • 8
  • 18
0

I prefer to save the last value set by our app, like this pseudocode:

// Save our last value
lastSoftwareVal = 0;

// Set value from program
slider_set_value(value){
    lastSoftwareVal = value;
    slider.setValue(value)
}

// Slider on change callback
slider_on_change(value){
    if(value == lastSoftwareVal){
        // Value was changed by our app
        return false;
    }
    // User has changed the value, do something
}
Cristian Deluxe
  • 130
  • 2
  • 12