Can we connect QPushButton to change values of QSlider?

1.2k views Asked by At

I have two push buttons labeled with + and - probably, I need to increase and decrease the values of the slider using these push buttons, Please help me to code this function.

2

There are 2 answers

5
eyllanesc On BEST ANSWER

First create slots plus() and minus():

public slots:
    void plus();
    void minus();

Then connect the clicked signal with the respective slot:

connect({your minus QPushButton}, SIGNAL(clicked()) , this, SLOT(minus()));
connect({your plus QPushButton}, SIGNAL(clicked()) , this, SLOT(plus()));

In each slot implement the increase or decrease tasks.

void {your widget}::plus()
{
    {your slider}->setValue({your slider}->value()+1);
}

void {your widget}::minus()
{
    {your slider}->setValue({your slider}->value()-1);
}
0
Kevin Krammer On

Alternatively, with a C++11 capable environment, with lambdas instead of new slots

connect(minusButton, &QPushButton::clicked, slider,
        [slider] () { slider->setValue(slider->value() - 1 );});
connect(plusButton, &QPushButton::clicked, slider,
        [slider] () { slider->setValue(slider->value() + 1 );});