Need to build a custom slider in qt 4

  • All,

    I need to create a relatively complex slider widget. It needs to be able to take multiple sliders per line, which would be able to move independently of each other.

    I've created a notional version of it just putting the pixels on the screen with the .paintEvent() method of my custom widget, but of course the "blocks" on slider don't move.

    @ #include <QtGui>

    #include "gnzprimtl.h"
    #include <iostream>

    GNZPrimTL::GNZPrimTL(QWidget *parent)
    : QWidget(parent)

     setWindowTitle(tr("Timeline Manager"));


    void GNZPrimTL::paintEvent(QPaintEvent *)

     QPainter painter(this);
     //top left is (0,0)
     //background and center stripe
     QColor black = QColor(0,0,0);
     QColor grey = QColor(127,127,127);
     QColor blue = QColor(0,0,255);
     QColor green = QColor(0,255,0);
     painter.drawRect(0, 0, width(), height());
     painter.drawRoundedRect(width()*.025, (height()/2)*.85, width()*.95, height()*.2, 5.5, 5.5);
     // block 1
     painter.drawRoundedRect(width()*.1, height()*.05, width()*.1, height()*.9, 5.5, 5.5);
     // block 2
     painter.drawRoundedRect(width()*.5, height()*.05, width()*.1, height()*.9, 5.5, 5.5);


    But of course the blocks don't move in response to mouse events. I'm supposing I need to derive the background-and-frame class from some kind of container widget, and then instantiate the blocks as a child of the container.

    Obviously not really sure where to start, though. Any help would be appreciated.

  • Your solution seems difficult. If I were you I would put several "QSliders": inside a "QHBoxLayout": (horizontal layout). Using "Qt style sheets": the appearance of the sliders can be customized.

  • Hi,

    Do you want something like "that": You can check the source code of the "ctkRangeSlider": of "Commontk":

  • For a slider with two handles you can use "QxtSpanSlider": For a slider with multiple handles take a look to QxtSpanSlider implementation. btw ... What I've done in the past, subclass QSlider and create any number of QLabel with QSlider parent and implement mousePress & mouseMove events for these labels. It's not so hard to calculate label position in slider min/max ranges.

Log in to reply

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