Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, December 2006
QSlider problem


Message 1 in thread

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

Message 2 in thread


> 
> 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
> 
> 

Hi Ashish,
I think Qt is pretty well set up to use signals and slots for exactly
this type of event. I'm sure there's a way to do it otherwise, but I
don't see how without defining your own callback function and
reimplementing some of QSlider's API. Is there some reason you don't
want to use signals and slots? I've found them to be extremely handy.

ry

--
 [ signature omitted ] 

Message 3 in thread

Hi Ryan,

Thanks for replying. I guess being a newbie, I haven't fully understood
signals and slots. Can you plz. tell how to do this using signals and slots?
Atleast that way I can get an understanding of it.

Thanks,
Ashish

On 12/11/06, Ryan Bobko <ryan@xxxxxxxxxxxxxxxxxxxxx> wrote:
>
>
>
> >
> > 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
> >
> >
>
> Hi Ashish,
> I think Qt is pretty well set up to use signals and slots for exactly
> this type of event. I'm sure there's a way to do it otherwise, but I
> don't see how without defining your own callback function and
> reimplementing some of QSlider's API. Is there some reason you don't
> want to use signals and slots? I've found them to be extremely handy.
>
> ry
>

Message 4 in thread

On 11.12.06 16:19:34, Ashish Singh wrote:
> Thanks for replying. I guess being a newbie, I haven't fully understood
> signals and slots.

Have you looked at the examples that come with Qt? They illustrate the
usage of signals/slots quite well.

> Can you plz. tell how to do this using signals and slots?
> Atleast that way I can get an understanding of it.

In your class (which needs to inherit from QObject or QWidget) add 

public slots:
  valueChanged(int);

> >> QSlider *slider = new QSlider(Qt::Horizontal);
> >> slider->setMinimum(1);
> >> slider->setMaximum(100);

connect( slider, SIGNAL(valueChanged(int)), this,
SLOT(changeValue(int)));

Then implement the valueChanged() function as you need it, the new value
of the slide is given as the int argument to that function.

You should also give "this" as parent to the QSlider so it will get
deleted when your widget is deleted.

Andreas

-- 
 [ signature omitted ] 

Message 5 in thread

Thanks for replying Andreas and giving me a simple example.
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?

Also, in the example you gave, as you said, I need to implement
valueChanged(int) function, right. And what about the changeValue function
for SLOT? What does that do?

On 12/11/06, Andreas Pakulat <apaku@xxxxxx> wrote:
>
> On 11.12.06 16:19:34, Ashish Singh wrote:
> > Thanks for replying. I guess being a newbie, I haven't fully understood
> > signals and slots.
>
> Have you looked at the examples that come with Qt? They illustrate the
> usage of signals/slots quite well.
>
> > Can you plz. tell how to do this using signals and slots?
> > Atleast that way I can get an understanding of it.
>
> In your class (which needs to inherit from QObject or QWidget) add
>
> public slots:
>   valueChanged(int);
>
> > >> QSlider *slider = new QSlider(Qt::Horizontal);
> > >> slider->setMinimum(1);
> > >> slider->setMaximum(100);
>
> connect( slider, SIGNAL(valueChanged(int)), this,
> SLOT(changeValue(int)));
>
> Then implement the valueChanged() function as you need it, the new value
> of the slide is given as the int argument to that function.
>
> You should also give "this" as parent to the QSlider so it will get
> deleted when your widget is deleted.
>
> Andreas
>
> --
> You're being followed.  Cut out the hanky-panky for a few days.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>

Message 6 in thread

On 11.12.06 17:23:08, Ashish Singh wrote:
> 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?

I can't give you specific examples without seeing some more code. Just a
scenario: Say I want to have a color chooser type of widget, then I'd
create a QWidget subclass, add 3 sliders to it, connect each slider to a
differn slot-member of my class. Also the class would get a pointer to
VTK color-object (or whatever VTK uses, I don't know that framework),
and in the slots I'd manipulate the color object depending on the
changes of the slider.

> Also, in the example you gave, as you said, I need to implement
> valueChanged(int) function, right. And what about the changeValue function
> for SLOT? What does that do?

I got it mixed up. valueChanged is a signal, that is emitted by the
QSlider every time the slider is moved. The current value is transmitted
together with the signal.

The connected slot changedValue(int) than executes whatever action
is associated with sliding the slider. In the above scenario you would
change the color-objects color in this slot.

I hope that clears things up, if not you really need to dive into the
examples and try to understand how they work. I'd suggest to begin with
the widgets examples.

Andreas

-- 
 [ signature omitted ]