0

I am using QSlider element in my app as a toogle switch. I took this project over from a colleage of mine who left. So far so good, except, that this switch has a black label around it. I can't really change the style and rework everything so, I am stuck with label and slider as switch. I need to handle events from button clicked and button released. I do it like this-

bool CustomSlider::eventFilter(QObject *obj, QEvent *ev )
{
    if (ev->type() == QEvent::MouseButtonRelease)
    {
        changeSliderValue();

    }
    if(ev->type() == QEvent::MouseButtonPress){
        lastSaved = ui->horizontalSlider->value();
    }
    return QWidget::eventFilter(obj,ev);

And changeSliderValue()-

void CustomSlider::changeSliderValue()
{
    int current = ui->horizontalSlider->value();
    if(lastSaved==current){
        if(current){
            ui->horizontalSlider->setValue(0);
        }else{
            ui->horizontalSlider->setValue(1);
        }
    }
}

So, in short, if I click, it saves state of slider and when releasing it checks against saved state. I do it because QSlider has button pressed signal as well. I have put debug messages in my code and funny thing is debug messages fire. So my code works. If I call ui->horizontalSlider->value() I get the correct(changed) value But the slider is not moving. I have tried checking sequences, if it does not change value twice(it does not), I have tried calling repaint() and so far nothing works. Except if don't minimize(via minimize button) or click in different app(like web browser) and then I can see that slider moves to correct postion. I am completely confused. Has anyone encoutered something like this before?

Them4
  • 1
  • 2
  • 1
    I have tried your code and it works, I have added some more things that I think it is obvious that you have placed, in the following link you can see my test code: https://github.com/eyllanesc/stackoverflow/tree/master/48853221 – eyllanesc Feb 18 '18 at 16:16
  • Works for me, too. Did you try calling update() or delaying the update using a timer? Does the label update correctly? What platform are you using, and which qt version? – SteakOverflow Feb 19 '18 at 08:20
  • Thank you everyone, appereantly I needed to call repaint(), but not in CustomSlider class, but in class where CustomSlider is placed :) – Them4 Feb 19 '18 at 11:02

0 Answers0