You have to put the argument int
on the signal signature to make it pass the value to the slot. Also, never put argument names in the SIGNAL(...)
/ SLOT(...)
signature specifications.
...
QSlider *slider = new QSlider(Qt::Horizontal, this);
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(value(int)));
...
Also, make sure that value
is a slot of your class, not a free-standing function. I guess you already put the code above in a class function, not in main or any other free-standing function, because in these there is no this
defined. So the slot you are talking about has to be a member function, especially a QObject slot of the class you are writing this code in. Change
void value (int k) {
cout << k << endl;
}
to
void MyClass::value (int k) {
cout << k << endl;
}
and in your class definition of MyClass
add a public slots:
section:
class MyClass : public ... {
Q_OBJECT
...
public slots:
void value(int);
...
}
Also, give your slot a meaningful name, for example sliderChanged
, otherwise, chaos will rule your project sooner or later for sure.