PDA

View Full Version : valueChanged signal not working with QSlider subclass



mlr
14th June 2011, 00:20
I am new to Qt and am having trouble getting the connect-SIGNAL-SLOT mechanism working. When I try to use the valueChanged signal with a QSlider subclass I get the error "No such signal SimpleSlider::valueChanged" on connect. If I try to do similar code with the sliderReleased signal connect returns true but the trace has the error message "QMetaObject::indexOfSignal: signal sliderReleased() from QSlider redefined in SimpleSlider" and my SLOT function never gets called. Below is my code. Please help.


#ifndef SIMPLESLIDER_H
#define SIMPLESLIDER_H

#include <QtGui>
#include "ui_simpleslider.h"

class SimpleSlider : public QSlider
{
Q_OBJECT

public:
SimpleSlider(Qt::Orientation orientation, QWidget *parent = 0);
~SimpleSlider();

signals:
void valueChanged(int value);
void sliderReleased();

private slots:
void valueHasChanged();

private:

};

#endif // SIMPLESLIDER_H



#include "simpleslider.h"

SimpleSlider::SimpleSlider(Qt::Orientation orientation, QWidget *parent)
: QSlider(orientation, parent)
{
bool retval = connect(this, SIGNAL(valueChanged(int value)), this, SLOT(valueHasChanged()));
retval = connect(this, SIGNAL(sliderReleased()), this, SLOT(valueHasChanged()));
}

SimpleSlider::~SimpleSlider()
{

}

void SimpleSlider::valueHasChanged()
{
QMessageBox::information(this, "SimpleSlider", "Value has Changed");
}

deyili
14th June 2011, 03:12
Singal valueChanged and sliderReleased have been declared in QSlider, so you shouldn't declare them again.

mlr
14th June 2011, 03:36
Thank you deyili. This solves the problem for the sliderReleased signal. I still have the error with the valueChanged signal that I get the error "Object::connect: No such signal SimpleSlider::valueChanged(int value) in simpleslider.cpp:6"

But now I can move ahead with my implementation.

deyili
14th June 2011, 08:02
you shoud code like this:
bool retval = connect(this, SIGNAL(valueChanged(int)), this, SLOT(valueHasChanged()));

PS:
Signals and slots are matched by string inside the Qt framework.

Santosh Reddy
14th June 2011, 08:04
Thank you deyili. This solves the problem for the sliderReleased signal. I still have the error with the valueChanged signal that I get the error "Object::connect: No such signal SimpleSlider::valueChanged(int value) in simpleslider.cpp:6"
This is because signal and slot should have same signature (declaration), in your case signal valueChanged(int) cannot be connected to valueHasChanged(), you can try connecting it to overloaded function valueHasChanged(int) (note the int as parameter, this is what your are missing)

mlr
14th June 2011, 18:35
This resolved the problem. Thank you! The question is now completely solved.

joyer83
14th June 2011, 20:17
This is because signal and slot should have same signature (declaration), in your case signal valueChanged(int) cannot be connected to valueHasChanged(), you can try connecting it to overloaded function valueHasChanged(int) (note the int as parameter, this is what your are missing)
I'm sorry, but this is not correct. You can connect signal valueChanged(int) to slot valueHasChanged().
The problem was that the signal's signature had argument's name in the connect call.
So, SIGNAL(valueChanged(int value)) should have been SIGNAL(valueChanged(int)).

Santosh Reddy
15th June 2011, 06:59
I'm sorry, but this is not correct. You can connect signal valueChanged(int) to slot valueHasChanged().
The problem was that the signal's signature had argument's name in the connect call.
So, SIGNAL(valueChanged(int value)) should have been SIGNAL(valueChanged(int)).

Yes, it is not correct, I will say it's not complete. The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.)