PDA

View Full Version : Signal and Slot -changing value of double or integer



samee999
20th August 2010, 18:06
Hello,

I am new to QT and I just wanted to ask if there is some way of changing the value of an integer as a slot when your signal is a QSpinBox

Basically I am trying to do something like this:

int rollangle ;
QSpinBox *spinrotate = new QSpinBox;
spinrotate->setRange(0,180);
QObject::connect(spinrotate,SIGNAL(valueChanged(in t)),rollangle,SLOT(setValue(int)));

Off course I realize this is not how its supposed to be done since, as far as I know, both signal and slot should be of the same type.

But I need the integer rollangle to change with spinrotate value.
Because I am implementing a Qtransform::rotate function after this and i want the rotation to change every time spinrotate is changed

I would be really grateful if someone can help me with this! :)

Urthas
20th August 2010, 18:11
Primitives like int do not have slots. All you need to do is connect the spinbox signal to a function that updates the value of rollangle:



connect(spinrotate, SIGNAL(valueChanged(int), this, SLOT(updateRollAngle(int));
...
void YourClass::updaterollAngle(int i)
{
rollangle = i;
}


By the way, you are sort of correct with regards to
both signal and slot should be of the same type

They have to have the same parameters, or, failing that, the signal may have more parameters in which case the extra ones are ignored by the slot. However, even in that case the parameters that are not ignored by the slot must be the same as those in the slot.

samee999
20th August 2010, 22:18
thanx a lot for ur post,

I am trying to do as u pointed out. so this is my code:
class faith1
{
public:
void updaterollAngle(int);
private:

};
void faith1::updaterollAngle(int i)
{
int rollangle;
rollangle = i;

}

in the int main function i do this :


faith1 andy;
QSpinBox *spinrotate = new QSpinBox;
spinrotate->setRange(0,180);

connect(spinrotate,SIGNAL(valueChanged(int)),this, SLOT(andy.updaterollAngle(int)));

this is off course not the complete code, but i get the following bug:

"invalid use of 'this' in non-member function"


i am sorry if there is a very lame reason that this is happening, (there must be), but i dont know exactly how to use the 'this' pointer. hope somebody can help,

p.s. i know , i suck at programming :D

Urthas
20th August 2010, 22:54
Read at least first half of the following link *thoroughly*: http://doc.trolltech.com/4.6/signalsandslots.html

When you are done, you will know how to structure the faith1 header file for what you want to do. Then, remove the rollangle declaration from your updaterollAngle() code. It should be a faith1 instance variable, declared privately in the header. In your connect statement, replace "this" with "&andy".

update: andy became &andy per Lykurg's good eye :p

Lykurg
20th August 2010, 22:55
Please read the documentation for signals and slots. You are missing the slot keyword as well as the Q_OBJECT macro. Further you want replace this with &andy.

samee999
21st August 2010, 15:07
Hello,

thanx a lot for your reply. I think i managed to figure it out. but now i have another issue, jus like i said i would like to use the rollangle variable to allow me to perform real time rotation of a dial.

While the variable rollangle does change since i call signal slot, for its update, the call to the function transform.rotate(andy.myvalue) is not called everytime there is a change in rollangle, so obviously the dial does nt rotate real time in this way. does any1 v an idea of how i can call transform.rotate() using a signal slot method so that it is updated everytime the variable rollangle changes?
if there is any other way i can perform this, that is off course also quite welcome :D

btw , myvalue is a public function in my class:

int myvalue() const { return rollangle; }

(andy is the name of the class instance)

Lykurg
22nd August 2010, 00:12
You can use it for real time operations. you just have to call an update function inside the slot. Or use a setter function in your class which performs an update if the value gets changed.