Need to build a custom slider in qt 4
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>
void GNZPrimTL::paintEvent(QPaintEvent *)
QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); //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.setBrush(black); painter.drawRect(0, 0, width(), height()); painter.setBrush(grey); painter.drawRoundedRect(width()*.025, (height()/2)*.85, width()*.95, height()*.2, 5.5, 5.5); // block 1 painter.setBrush(green); painter.drawRoundedRect(width()*.1, height()*.05, width()*.1, height()*.9, 5.5, 5.5); // block 2 painter.setBrush(blue); painter.drawRoundedRect(width()*.5, height()*.05, width()*.1, height()*.9, 5.5, 5.5); return;
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":http://qt-project.org/doc/qt-5.0/qtwidgets/qslider.html inside a "QHBoxLayout":http://qt-project.org/doc/qt-5.0/qtwidgets/qhboxlayout.html (horizontal layout). Using "Qt style sheets":http://qt-project.org/doc/qt-4.8/stylesheet.html the appearance of the sliders can be customized.
Do you want something like "that":http://www.commontk.org/images/e/e1/CtkRangeSlider.png? You can check the source code of the "ctkRangeSlider":http://www.commontk.org/docs/html/classctkRangeSlider.html of "Commontk":http://www.commontk.org/index.php/Main_Page.
For a slider with two handles you can use "QxtSpanSlider":http://libqxt.bitbucket.org/doc/tip/qxtspanslider.html#details 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.