New double slider editor

Aug 26 2009 Published by under Pyface, Qt, Traits

Occasionally I want a slider to control 2 values, such as defining a date range or clipping parameters. In the past, I would just create 2 sliders using the RangeEditor. It worked fine, but took up 20 or so pixels and didn’t really visually show the relationship between the 2 variables.

BoundsEditor screen shot

BoundsEditor screen shot

Qt and PyQt made it ridiculously easy to create the control I wanted. I started off transcoding the Qt QSlider C++ code into python, then added the second slider. In all, its about 180 lines of Python, less than 50 of it specific to the seconds slider.

I also wrapped this into a Traits editor, which can be found at enthought.traits.ui.qt.extra.bounds_editor.BoundsEditor

12 responses so far

  • avatar J-Chris says:

    Where exactly could I see the implementation ? Thks

  • avatar Bryce Hendrix says:

    Here is my test code, which also includes a button to increment the leftmost slider:


    from enthought.traits.api import HasTraits, Range, Int, Button, Float
    from enthought.traits.ui.api import View, Item
    from enthought.traits.ui.qt4.extra.bounds_editor import BoundsEditor

    class Foo(HasTraits):
    bounds = Range(10,20,value=15)
    incr_low = Button("incr_low")
    first = Float(10)

    traits_view = View(Item('bounds', editor=BoundsEditor(low_name='first', high=20)),
    Item('incr_low'))

    def _incr_low_changed(self):
    self.first += 1

    def _first_changed(self, old, new):
    print "new low:", new

    Foo().configure_traits()

  • avatar Bryce Hendrix says:

    of course, wordpress screwed up the formatting of my code, so beware…

  • avatar Turbo says:

    Hello! I love this widget and it’s exactly what I need, however I don’t know how to add it to my QtDesigner… I know this is a n00b question, could you please point me to some good reading on the subject? Thanx! :)

  • avatar bryce hendrix says:

    I no next to nothing about QtDesigner. You’ll probably need to look at the BoundsEditor source and strip out the control, then follow the instructions on Riverbank’s website

    http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#writing-qt-designer-plugins

  • avatar Hualei Chen says:

    Thanks for the demo. It presents a really nice way to implement a double slider. We need the similar widget in our program and I made my own version in C++ with Qt4. A recorded video was uploaded to http://www.youtube.com/watch?v=zHIyUd6Q28U

  • avatar John Smith says:

    Hi Bryce. Do you have something similair using wxPython?

  • avatar John Smith says:

    I misspelled the e-mail address sorry

  • avatar bryce says:

    John,

    I did implement a wx version as well, but its not nearly as nice. If I remember correctly its just 2 sliders, stacked vertically.

  • avatar John Smith says:

    Bryce,
    Thanks for your reply. I have done the two sliders stacked vertically also but as you have noted it really is cumbersome. Your single slider is elegant. I think I will probably try to “roll my own” then. Again thanks

    • avatar Bryce Hendrix says:

      John, I was thinking about why I had to stack them- I tried putting 2 sliders on top of each other, but I had problems getting events on the bottom slider. Maybe you can make one slider the parent of the other & Skip the event so the parent gets it?

      In any case, I recommend Qt if possible :)

  • avatar vane says:

    Very interesting, but the widget itself is not very useful without valueChanged signals for the min/max/span

Leave a Reply

Featuring Advanced Search Functions plugin by YD