PDA

View Full Version : initial position of a QSlider



franco.amato
22nd September 2008, 23:51
Hi to all,
I'm building a little application where I need a QSlider. When I start the application I need the cursor to be positioned in the half of the slider but this doesn't happens.
When I construct the window I set slider->setValue(5) //the range is 0...10, so 5 is the half
but when I run the application the cursor is always positioned in the position 0.
I don't know why.

Regards,
Franco

aamer4yu
23rd September 2008, 05:19
Can you show the code how you are doing it ?

Thoosle
23rd September 2008, 16:31
Franco,

Take a look at the QSlider class methods setSliderPosition ( int ) and setTracking ( bool ) ....

franco.amato
23rd September 2008, 16:55
Hi Thoosle,
I gave a look here http://doc.trolltech.com/3.3/qslider.html about QSlider class, but there is no a setSliderPosition method :(
May be I'm searching in the bad place?

Regards,
Franco

jpn
23rd September 2008, 17:16
I'm sure QSlider::setValue() works just fine. The problem must be elsewhere. Why don't you show the relevant code? Otherwise we can do nothing but guess.

franco.amato
23rd September 2008, 17:51
Hi,
I post here the code where I construct the slider:


QSlider *Window::createSlider()
{
QSlider *slider = new QSlider(0, 10, 1, 5 ); //5 = initial value
slider->setSingleStep(1);
slider->setPageStep(1);
slider->setTickPosition(QSlidet::TicksRight);

return slider;
}

And here I call the createSlider() routine:


Window::Window()
{
cout << "Window::Window : window object created." << endl;

coinWidget = new CoinWidget; // the coin3d viewer
volumeSlider = createSlider(); //here I construct the slider
openAudioFileButton = createButton();

connect(volumeSlider, SIGNAL(valueChanged(int)), coinWidget, SLOT(setAudioTrackVolume(int)));
connect(openAudioFileButton, SIGNAL(released()), coinWidget, SLOT(openAudioTrack()));

QHBoxLayout *mainLayout = new QHBoxLayout( this );

mainLayout->addWidget(coinWidget);
mainLayout->addSpacing( 10 );
mainLayout->addWidget(volumeSlider);
mainLayout->addSpacing( 30 );
mainLayout->addWidget(openAudioFileButton);
mainLayout->addSpacing( 30 );
setLayout(mainLayout);
setWindowTitle(tr("Sound Test Environment"));
}

That's all.
I'm sure there is some errors in my code but I can't find it.
I have an other question:
Is possible to create a slider with smoth movement? I hope you can understand what I mean because my english's not perfect.

Regards,
Franco

Thoosle
23rd September 2008, 18:59
Franco,

Sorry I wasn't paying attention to which version you're using. setSliderPosition method is in QT 4.

This shouldn't work but have you tried setting the "value" after the QSlider has been instantiated? If for some strange reason the QSlider wasn't being initialized properly this could show??

franco.amato
23rd September 2008, 19:10
Thoosle yes I'm using QT 4.
I followed your suggestion and now it works, but I don't understand why :(.
Is possible to have a smooth movement of the cursor?

Regards,
Franco

Thoosle
23rd September 2008, 20:09
Franco,

If you're using Qt4 then you need to look at the Qt4 docs, not Qt3. The QSlider constructor you show in your code is incorrect for Qt4. This is probably why it doesn't instantiate properly and why it works setting value after instantiation. QSlider has two constructors in Qt4 as follows:


QSlider ( QWidget * parent = 0 )

QSlider ( Qt::Orientation orientation, QWidget * parent = 0 )

try this link http://doc.trolltech.com/4.3/qslider.html

franco.amato
24th September 2008, 18:37
Franco,

If you're using Qt4 then you need to look at the Qt4 docs, not Qt3. The QSlider constructor you show in your code is incorrect for Qt4. This is probably why it doesn't instantiate properly and why it works setting value after instantiation. QSlider has two constructors in Qt4 as follows:


QSlider ( QWidget * parent = 0 )

QSlider ( Qt::Orientation orientation, QWidget * parent = 0 )

try this link http://doc.trolltech.com/4.3/qslider.html

Thanks for the link.
I didn't see the reference to qt 3 .
Regards,
Franco