Search code examples
qtqmlqtquickcontrols

Adding Functionality / behavior to a QML Slider.


Is it possible to create a subclass of a QML element?

I was trying to create a custom QML slider (as opposed to the one available in QtQuick.Controls). So I wanted to look and feel to remain the same while the range itself to behave logarithmically (not arithmatically).

I know it is possible for me to define a custom slider in c++, register it with QML and then use it in QML. But I wanted to see if i could reuse the existing QML slider by creating a subclass so that I can change only what I want to change and everything else behaves the same as the QML Slider.

So. Is it possible to create a custom subclass of a QML element.

Thank you


Solution

  • You just have to create a new qml file

    say you name it MySlider.qml.

    MySlider.qml :

    import QtQuick 2.0
    import QtQuick.Controls 1.0
    
    Slider {
        // anything you want
    }
    

    Now you can use MySlider as a new component

    main.qml

    ...
    ...
    MySlider {
    }
    ...
    ...