PDA

View Full Version : QSlider problem



Ashish
11th December 2006, 22:22
Hi,

I am a newbie in Qt. I am trying to use QSlider to control the color of a 3D volume. But I having a hard time figuring it out. This is what I am doing or trying to do:
This is only a part of my code-


int mvar1;
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setMinimum(1);
slider->setMaximum(100);
if(slider has moved )
{
myvar1 = get new value of slider; // this value then goes into another function to control the color
}

Can anyone guide me how to do this without using signals and slots? How do I figure out if the slider has moved? Is there a function that gives me the status of the slider? I went through the documentation but had a hard time understanding it. What is the easiest way of doing this?

Thanks,
Ashish

wysota
11th December 2006, 22:30
The easiest way of doing this is signals and slots :)
Simply make that "another function to control the color" a slot and connect it to the slider's signal.

Ashish
11th December 2006, 23:02
Thanks for replying wysota.
With signals and slots, the sender and receiver should be pointers to QObject, right. Now my color variable goes to a function that is a part of another class which is a part of completely different API(VTK). I am using 2 different APIs in the same application, Qt and VTK. The function to control color looks like-AddRGB(r,g,b) and r is the value that I want to control with the slider. But this AddRGB function is a part of the VTK API.

In this case how do I do it?

wysota
11th December 2006, 23:06
Create a dummy QObject which will simply call your actual method, like this:

class SignalProxy : public QObject {
Q_OBJECT
public:
SignalProxy(VTKObject *o, QObject *parent=0) : QObject(parent){
_o = o;
}
public slots:
void receive(int value){
if(!_o) return;
o->doSomething(value);
}
private:
VTKObject *_o;
};

Ashish
11th December 2006, 23:34
Thanks wysota.
So how would my connect function look then?
would it be-
connect(slider, SIGNAL(valuechanged(int)), mysignalproxy, SLOT(dosomething(value)) );

where-mysignalproxy is a pointer to SignalProxy
and function dosomething(value) would take the new value and call AddRGB(value,g,b) within it.
Is this how it works, to make sure I understand it, correctly?

wysota
12th December 2006, 01:08
Yes, you understand it correctly. Just remember that the signal is called "valueChanged", not "valuechanged". Also remember that connect() is a method of QObject, so you'll probably have to call it as QObject::connect or mysingalproxy->connect as you'll probably be doing it from outside of any QObject.