3

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

OneRandomCoder
  • 87
  • 2
  • 11
  • Why not just define a new `property double logValue: Math.log(value)` in the normal slider and then use this one? – koopajah May 05 '14 at 11:10
  • Thats a good idea for this specific problem. But in general I wanted to know if it is possible to create a subclass of a QML element. – OneRandomCoder May 05 '14 at 11:40
  • Possible as hell, but may be useless, if customiazation-via-subclassing behavior is not there in the base item. – mlvljr May 07 '14 at 19:05

1 Answers1

2

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 {
}
...
...
BlueMagma
  • 2,392
  • 1
  • 22
  • 46