1

Possible Duplicate:
Using QSlider to change the value of a variable

I'm trying to use the QSlider to to change a variable value: here is a part of the code :

 ....
QSlider *slider = new QSlider(Qt::Horizontal,0);
connect(slider,SIGNAL(valueChanged()),this,SLOT(value(int k)));
...

the function value is a SLOT that I want to use let's say like this:

 void value (int k ) {
cout<< k << endl;
}

the problem that I have is that nothing happens when I move the slider. ?

thanks in advance

Community
  • 1
  • 1
Engine
  • 5,360
  • 18
  • 84
  • 162
  • I would recommend you reading about signal and slots system in Qt AGAIN, as clearly you didn't understand it well – Kamil Klimek Jan 02 '13 at 13:12
  • No it's not a duplicat , I can use the silder value if I put it with a progress bar but I want to get the position of the slider – Engine Jan 02 '13 at 13:18
  • It's exactly same problem but in different place in code. And both problems have exactly same source: you didn't pay attention while you were reading tutorials/docs from Qt – Kamil Klimek Jan 02 '13 at 15:13

2 Answers2

11

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.

leemes
  • 44,967
  • 21
  • 135
  • 183
  • @Engine Ah, and pass a `parent` to the constructor of the `QSlider`. Otherwise, your application leaks memory. See my edit. – leemes Jan 02 '13 at 13:46
  • 2
    @Engine Oh, never mind... If you put the slider in a widget afterwards, it's ok to pass `0` in the constructor. Which I'm guessing you do, otherwise you see a single slider in a small window. – leemes Jan 02 '13 at 13:48
0

From the qt doc:

http://doc.qt.digia.com/qt/qabstractslider.html#valueChanged

void QAbstractSlider::valueChanged ( int value ) [signal]

I'm guessing that at runtime it cannot find the valueChanged() signal, so maybe changing:

...
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(value(int)));
...

Hope that helps.

lucasmrod
  • 260
  • 2
  • 9