*WARNING* - this solution is a massive hack job....but so far it is kinda doing what i want it to...just not 100%....clicking on the slider to get either of the heads is fine, but there is something wrong with my mouseMoveEvent.....i can't get it to let you drag the heads.....the code i am posting has been added to the soln given by wysota pretty much as-is.....his (oooh...bad assumptions I know....but for the ease of referral i'll go with "he" unless i know otherwise.....) repaint is exactly what I needed
i have not reimplemented mouseReleaseEvent because i can't see how it would help
I have created my own style to allow reimplementation of drawComplexControl (complex is right :P)
//mousePressEvent works OK
{
int newPosition = this->pixelPosToRangeValue(ev->pos().y());
emit newPos(newPosition);
bool closerMax = (qAbs((newPosition-this->value()))>qAbs((this->maximum()-newPosition-this->secondVal)));
//find out if the click was closer to the max or min slider head to know which one to move
//this is also useful because it means you cannot get the minimum head to go above the
//maximum one --> in my drawComplexControl I have managed to get the heads drawn in
//different colours
emit closer(closerMax);
if(closerMax)
{
this->secondVal = this->maximum()-newPosition;
}
else
{
this->setValue(newPosition);
}
this->update();
if (this
->pressedControl
== QStyle::SC_SliderHandle) setSliderDown(true);
this->snapBackPosition = this->position;
}
//mouseMoveEvent does not seem to do anything :(
{
if (this
->pressedControl
!= QStyle::SC_SliderHandle ||
(ev
->buttons
() & Qt
::RightButton)) { ev->ignore();
return;
}
ev->accept();
int newPosition = this->pixelPosToRangeValue(ev->pos().y());
emit newPos(newPosition);
bool closerMax = (qAbs((newPosition-this->value()))>qAbs((this->maximum()-newPosition-this->secondVal)));
emit closer(closerMax);
int m
= style
()->pixelMetric
(QStyle::PM_MaximumDragDistance,
&opt,
this);
if (m >= 0) {
r.adjust(-m, -m, m, m);
if (!r.contains(ev->pos()))
newPosition = this->snapBackPosition;
}
if(closerMax)
{
this->secondVal = this->maximum()-newPosition;
}
else
{
this->setValue(newPosition);
}
repaint();
}
//mousePressEvent works OK
void DoubleSlider::mousePressEvent(QMouseEvent *ev)
{
int newPosition = this->pixelPosToRangeValue(ev->pos().y());
emit newPos(newPosition);
bool closerMax = (qAbs((newPosition-this->value()))>qAbs((this->maximum()-newPosition-this->secondVal)));
//find out if the click was closer to the max or min slider head to know which one to move
//this is also useful because it means you cannot get the minimum head to go above the
//maximum one --> in my drawComplexControl I have managed to get the heads drawn in
//different colours
emit closer(closerMax);
if(closerMax)
{
this->secondVal = this->maximum()-newPosition;
}
else
{
this->setValue(newPosition);
}
this->update();
if (this->pressedControl == QStyle::SC_SliderHandle)
setSliderDown(true);
this->snapBackPosition = this->position;
}
//mouseMoveEvent does not seem to do anything :(
void DoubleSlider::mouseMoveEvent(QMouseEvent *ev)
{
if (this->pressedControl != QStyle::SC_SliderHandle || (ev->buttons() & Qt::RightButton)) {
ev->ignore();
return;
}
ev->accept();
int newPosition = this->pixelPosToRangeValue(ev->pos().y());
emit newPos(newPosition);
bool closerMax = (qAbs((newPosition-this->value()))>qAbs((this->maximum()-newPosition-this->secondVal)));
emit closer(closerMax);
QStyleOptionSlider opt = this->getStyleOption();
int m = style()->pixelMetric(QStyle::PM_MaximumDragDistance, &opt, this);
if (m >= 0) {
QRect r = rect();
r.adjust(-m, -m, m, m);
if (!r.contains(ev->pos()))
newPosition = this->snapBackPosition;
}
if(closerMax)
{
this->secondVal = this->maximum()-newPosition;
}
else
{
this->setValue(newPosition);
}
repaint();
}
To copy to clipboard, switch view to plain text mode
if you have any suggestions that would be super
when i have the class of double headed slider worked out nicely - maybe with a flexible style - i'll upload it here because it seems like something a few ppl have wanted in the past and never really sorted out.....
Bookmarks