Using QSlider to change the value of a variable

2020-05-08 08:03发布

问题:

I'm trying to use QSlider to change the value of a variable,

 #include <QSlider>

class MainThread : public QWidget{
Q_OBJECT
public:
MainThread(QWidget *parent=0);
private slots:
    void setValue(double);
private:
QSlider *slider; 
};

MainThread::MainThread(QWidget *parent):QWidget(parent){
    slider = new QSlider(Qt::Horizontal,0);
    connect(&slider, SIGNAL((slider->valueChanged())),
             this, SLOT(setValue(double))); // here's my  problem 
    ...
}

My question is how can I connect the SIGNAL of the slider to the setValue(double) SLOT.

Thanks in advance.

回答1:

slider is already a pointer, e.g. remove the '&'

connect( slider, SIGNAL((slider->valueChanged())), this, SLOT(setValue(double)) );

Edit: This won't work, since the signal has no argument. Rename the setValue(double) to setValue() and get the value from the slider with slider->value().