Create a video player slider seeker with QSlider

  • I'm using a library to create a video player with Qt and this library returns for me the video current position as float and also as a percentage, for example, this video has the length of 5568 and the event/signal positionChanged returns me this:


    It means that the library only returns the player current position from 0.0 to 1.0 (not precise, in this video case it would be 0.0358617 to 1.01173).

    I was wondering. What is the best way to implement a slider for this?

    Another thing is: I have to implement the events to the user so he can slider and set the video position, the library has a method I created called setPosition(float) that will also accepts a float position as percentage from 0.0 to 1.0.

    Any ideas of how I can implement a QSlider to work with floats and work like a QProgress with percentage, also allow it to be changeable by user mouse events to return me float points to set to the library (from 0.0 to 1.0).

  • Lifetime Qt Champion

    Something like

    class DoubleSlider : public QSlider {
        DoubleSlider(QWidget *parent = 0) : QSlider(parent) {
            connect(this, SIGNAL(valueChanged(int)),
                this, SLOT(notifyValueChanged(int)));
        void doubleValueChanged(double value);
    public slots:
        void notifyValueChanged(int value) {
            double doubleValue = value / 10.0;
            emit doubleValueChanged(doubleValue);

    Its a bit of cheat as internally its range 0-10 but we divide with 10 before emitting the value;

  • I don't understand why you connected the int to the float as I don't intend to use the int method from QSlider. The idea would be to connect the media player to set the QSlider value directly. Idk, I'll see what I can do about that.

    The other thing is the range of the QSlider, I have to set it to 1.0 max as float, isn't it, but how?

  • Lifetime Qt Champion

    Internally the range is ints
    we divide by 10 and
    0.1 to 1.0
    and emit that as a signal. ( the new notifyValueChanged)
    you can connect setPosition(float) to it and it should just work.

    you set max as 10. it will become 0.10

    Well you can also make a value() that returns float and use that.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.