PDA

View Full Version : Signals and Slots



Maluko_Da_Tola
28th July 2010, 00:59
Hi

I created a variable int c,whose value is to be updated through a slider, taking the value of the slider. The variable c will be then converted to a Qstring and output as the text of a QPushButton. I used the following code in the main.cpp. However, it does not work, since the button always displays the initial value of the slider, which suggests that the variable c is not being updated.



QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 130);

QObject::connect(slider, SIGNAL(valueChanged(int)),
slider, SLOT(setValue(int)));

int Contador = slider->value();
QPushButton *butao = new QPushButton;
butao->setText(QString::number(Contador));


Any suggestions why this is not working?

Cheers

Maluko_Da_Tola
28th July 2010, 01:05
Sorry,The variable int c is defined as int Contador instead!

Cheers

ChrisW67
28th July 2010, 04:18
Hi


QObject::connect(slider, SIGNAL(valueChanged(int)),
slider, SLOT(setValue(int)));


This is almost certainly not what you wanted to do: setting the slider's value every time the slider changes.

If the code fragment you posted appears as one chunk of continuous code then you have bigger problems. Can you post a small, complete program?

grabalon
30th July 2010, 00:57
You'll want to add this to your *.h file (replace MyClass with your class name):

MyClass {
public:
...
private:
int c;
private slots:
void setVal(int);
}

implement the slot as follows:

void MyClass::setVal(int value)
{
c = value;
}

finally, change your connection to this:


QObject::connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setVal(int)));

That ought to get you started