3

I need something like a range slider (a slider that has two handles for setting min/max values).

I noticed that there is a RangeSlider in QtQuick and QML (Whatever that is), but there is not a 'standard widget' such as QRangeSlider.

Why is this?

Can I use that RangeSlider from QtQuick without writing QML code?

dtech
  • 47,916
  • 17
  • 112
  • 190
manatttta
  • 3,054
  • 4
  • 34
  • 72

3 Answers3

6

In my project I successfully used the Common TK range slider.

It looks like this:

enter image description here

Practical class ready to go here: CPP and header

Massimo Callegari
  • 2,099
  • 1
  • 26
  • 39
  • Was this style defined by you, or is it the default for the Operating System? Or worse, is this style defined by that widget? – manatttta Mar 16 '17 at 17:31
  • That's a screenshot taken from the Common TK website (http://www.commontk.org/index.php/Documentation/ImageGallery). It looks like the Gnome style, but I think you can customize it with Qt stylesheets – Massimo Callegari Mar 16 '17 at 17:55
  • In my application it looks like this (was using Linux KDE): http://www.qlcplus.org/features/vcxypad.png. In any case the style is OS-dependent – Massimo Callegari Mar 16 '17 at 18:02
  • I tried this and it actually very, very good! Also, I noticed your project has lots of great widgets. Jave you considered making a separate lib of widgets? – manatttta Mar 16 '17 at 18:38
  • Not really no. Most of them are tailored for specific usages in my software. Anyway, it's open source, so grab whatever you want/need – Massimo Callegari Mar 16 '17 at 18:40
  • I am using QT 5.11 and I can't see both of handles. I only see one handle. – mucisk Mar 27 '19 at 07:41
3

Since RangeSlider is a QML type you need QML context to use it in. QQuickWidget is just a QWidget that specializes in loading and displaying QML contents. The fact that it's just another subclass of it enables you to integrate it into a QWiget of your choice. See here for an example of how to integrate these two (the example uses QMainWindow but you can adapt it to your needs.

My guess for this missing UI component in a standard QWidget would be that:

  • it can easily be created by the developer by tweaking the QSlider (example)
  • the QWidget-related stuff in terms of new features from the Qt Company is basically dead since they go full speed towards QML. I don't like it but that's how things are
  • range sliders aren't that popular. You can easily replace a slider (that needs to support a range) with two sliders (one for lower and one for upper limit), two QSpinBoxes etc.

Better solution would be to dump the QML stuff for this and implement your own version in C++.

Community
  • 1
  • 1
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • `QGraphicsScene` is not use anymore as the backend of Qt Quick since Qt 5. – Benjamin T Mar 16 '17 at 16:06
  • You are right. I have forgotten about that. But you can still use a `QQuickWidget` to load the QML content inside a `QWidget` and make the required connections between the QML and C++ code. – rbaleksandar Mar 16 '17 at 16:12
  • 1
    That is a terrible idea! Creste a qml context to show one widget!? Why dont you load up webkit while you are at it, or hell start an instance of python. – Mr. Developerdude Mar 18 '17 at 05:48
  • I never said "Do this. It's the best solution". Imho the solution (which is also the accepted answer) is to implement the slider in C++. PS: RangeSlider is not a widget, it's a QML control since we are that touchy. – rbaleksandar Mar 18 '17 at 08:21
2

Because Qt Widgets is an "old" library and Qt developers, in particular The Qt Company, do not work on it beside the necessary bug fixing.

Whereas Qt Quick Controls (1 and 2) is more recent and get all the attention.

Moreover there is no link between the contents of Qt Quick and Qt Widgets, they are totally unrelated libraries, i.e a change or update to one will not lead to a change or update of the other.

(That's not totally true because Qt Quick Controls 1 uses Qt Widgets to do some styling, but as far as publicly available components are concerned this holds)

You generally cannot mix Qt Widgets and Qt Quick components, they are rendered in completely different ways, Qt Widgets use QPainter while Qt Quick use the SceneGraph. But you can render a Qt Quick scene inside a Qt Widgets app using QQuickWidget, so you could make a RangeSlider widget that internally shows a Qt Quick RangeSlider.

Benjamin T
  • 8,120
  • 20
  • 37
  • 1
    How do I use one of those widgets from QtQuick then? – manatttta Mar 16 '17 at 16:00
  • @manatttta I've update my answer, it should answer your question. – Benjamin T Mar 16 '17 at 16:02
  • "Because Qt Widgets is an "old" library and Qt developers, in particular The Qt Company, do not work on it beside the necessary bug fixing." that's totally wrong, as well as "You generally cannot mix Qt Widgets and Qt Quick components" – Gojir4 Oct 09 '18 at 12:38
  • 1
    @Gojir4 In 2012, ~since Qt 5 release, Qt Widgets were officially "marked as ‘done’, which means [they] don’t have anybody actively working on new feature". Source: http://blog.qt.io/blog/2012/04/18/qt-5-c-and-qt-widgets/ Only recently, in Qt 2018 roadmap, the Qt Company mentioned an increased activity around Qt Widgets (this goes along with a change in the mainteners of the module). But they still have not announced any new feature. On the contrary Qt Quick Controls get a batch of new controls for each Qt 5 minor release. So now enlighten me on how my statement is wrong. – Benjamin T Oct 09 '18 at 13:20
  • @Gojir4 Also if you have any mean of mixing Qt Widgets and Qt Quick Items as easily as mixing only Widgets or mixing only Items while preserving proper rendering order of elements across widget/item boundary using only Qt public API, please go ahead and prove me wrong. – Benjamin T Oct 09 '18 at 13:22
  • @BenjaminT `QQuickWidget *view = new QQuickWidget; view->setSource(QUrl::fromLocalFile("myqmlfile.qml")); view->show();` I don't see how it can be easier. – Gojir4 Oct 12 '18 at 14:29
  • @Gojir4 Read the documentation, there is a whole part about "Limitations": http://doc.qt.io/qt-5/qquickwidget.html#limitations and another part about backend incompatibility: http://doc.qt.io/qt-5/qquickwidget.html#support-when-not-using-opengl Plus several notes in other parts telling about other limitations. And if you think that I did not know about `QQuickWidget`, that means that you have not even read my answer... – Benjamin T Oct 12 '18 at 14:38