Results 1 to 17 of 17

Thread: Customize tick labels in a QwtSlider

  1. #1
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Customize tick labels in a QwtSlider

    I'm using QwtSlide to represent some values. For example if I want to represent 500, 1500, 2500... 9500, I'm getting a slider drawn from 500 to 9500, but labels appear only in 100, 2000, ... 9000.

    Code:
    Qt Code:
    1. ui.timeSampleSlider->setScale(500, 9500, 1000);
    2. ui.timeSampleSlider->setRange(500, 9500, 1000);
    3. ui.timeSampleSlider->setValue(500);
    4. ui.timeSampleSlider->setEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    Looks like this:
    gWy4D.jpg

    I want all my possible values (500, 1500, ... , 9500) to be labeled instead of these. I also tried this:

    Qt Code:
    1. QList<double> ticks [3];
    2. ticks[0]<<0.5<<1.5<<2.5<<3.5<<4.5<<5.5<<6.5<<7.5<<8.5<<9.5;
    3. QwtScaleDiv fixedScale(QwtInterval(start, end), ticks);
    4. QwtScaleDraw* scaleDraw = new QwtScaleDraw();
    5. scaleDraw->setScaleDiv(fixedScale);
    6. ui.timeSampleSlider->setScaleDraw(scaleDraw);
    7. ui.timeSampleSlider->setRange(start, end, interval);
    8. ui.timeSampleSlider->setValue(start);
    9. ui.timeSampleSlider->setEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    And looks even worse:
    l7FRO.jpg

    Any idea?

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    Why setting a tick at 0.5 when you want to have one at 500 ?

    Uwe

    PS: you can use QwtSlider::setScale( const QwtScaleDiv & )

  3. #3
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    Why setting a tick at 0.5 when you want to have one at 500 ?
    Because it represents time, and I work with milliseconds and want labels to look as seconds. Anyway that's a different issue I'll try to solve next. This is what happens when you inherit code

    PS: you can use QwtSlider::setScale( const QwtScaleDiv & )
    Anyway, what would be the difference with the setScaleDraw I tried? Could you give an example of usage? Thanks

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    Quote Originally Posted by RomanRdgz View Post
    Because it represents time, and I work with milliseconds and want labels to look as seconds.
    Then set the ticks to 500, 1000 and modify how the tick labels are represented:

    Qt Code:
    1. virtual QwtText YourScaleDraw::label( double value ) const
    2. {
    3. return QwtScaleDraw::label( value / 1000.0 );
    4. }
    To copy to clipboard, switch view to plain text mode 

    HTH,
    Uwe

  5. #5
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    That could work to control what's written into each label. I am overriding a method from Qt. Is it OK if I declare this overriden method into my own code, or do I need to do anything special?

    Anyway I still don't know how to tell the QwtSlider where to put labels, only how to format them. How can I do that?

  6. #6
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    Usually it's done this way:
    Qt Code:
    1. ui.timeSampleSlider->setScale( 500, 9500, 500 );
    2. ui.timeSampleSlider->setRange( 500, 9500 );
    3. ui.timeSampleSlider->setScaleMaxMinor( 0 ); // when you want to disable the minor ticks
    To copy to clipboard, switch view to plain text mode 
    Of course you can also calculate and assign the ticks manually ( even if I don't see any good reason in your situation ) using a QwtScaleDiv object, but then code has to be like this:

    Qt Code:
    1. QList< double > ticks[ QwtScaleDiv::NTickTypes ];
    2. ticks[ QwtScaleDiv::MajorTick ] << 500 << 1000 << 1500 << ... << 8000 << 8500 << 9000 << 9500;
    3.  
    4. ui.timeSampleSliders->setScale( QwtScaleDiv( 500, 9500, ticks ) );
    5. ui.timeSampleSlider->setRange( 500, 9500 );
    To copy to clipboard, switch view to plain text mode 
    Uwe

  7. #7
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    But I have already tried the first code (but with 1000 between each tick, not 500 as you write here), and I'm getting the slide I put an image about, with ticks on 1000, 2000, 3000, ... 9000, when my selectable points are in 500, 1500, 2500, ... 9500. I still don't know how to solve that.

  8. #8
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    By replacing 1000 by 500 - or if you want to have the ticks on 500, 1500 only ( but not on 1000 ) the second option I have posted ( of course without the ticks at 1000, 2000 ... ) is what you need.

    Uwe
    Last edited by Uwe; 7th September 2012 at 10:39.

  9. #9
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    I have and error when using setScale method: none of the 4 overloads could convert all the argument types.

    Qt Code:
    1. QList<double> ticks [QwtScaleDiv::NTickTypes];
    2. for(int i=0; i<timeSamples.size(); i++)
    3. ticks[0].append(timeSamples.at(i));//<<0.5<<1.5<<2.5<<3.5<<4.5<<5.5<<6.5<<7.5<<8.5<<9.5;
    4. ui.timeSampleSlider->setScale(QwtScaleDiv(start, end), ticks);
    5. ui.timeSampleSlider->setRange(start, end);
    6. ui.timeSampleSlider->setScaleMaxMinor(0);
    7. ui.timeSampleSlider->setValue(start);
    8. ui.timeSampleSlider->setEnabled(true);
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    Of course not - ticks is a parameter of the QwtScaleDiv constructor. Are you familiar with C++ ?

    Uwe

  11. #11
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    Ok, it was a mistake while reading. SHould have copy-pasted your lines.

    Anyway it doesn't work. It looks just as my second test (see second image at the first post). And it allows user to stop at any possible value into the range, which didn't before. I guess that's for not using the third parameter at setRange method.

    Any other possible approach?

  12. #12
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    Quote Originally Posted by RomanRdgz View Post
    Anyway it doesn't work.
    When you assign the values from your timeSamples array as ticks, then you get these values as ticks. When you don't want them don't assign them.
    Come on - this thread is about 3 simple lines of code - all you need to do is to copy what I have posted.

    Uwe

  13. #13
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    I am assigning the values I want for my ticks. That's what I was trying to do myself even before posting this. I have copied your lines of code exactly and if my input ticks are 500, 1500, 2500... 9500, the only labels I'm getting are 2000, 4000, 6000 and 800, just as at the beginning.

    My code is the one I shown two answers ago, when I had a mistake with the setScale, but now that method is jsut as you suggested. Doesn't work.

    Qt Code:
    1. QList<double> ticks [QwtScaleDiv::NTickTypes];
    2. for(int i=0; i<timeSamples.size(); i++)
    3. ticks[0].append(timeSamples.at(i)); //Timesamples has inside: 500, 1500, 2500...9500
    4. ui.timeSampleSlider->setScale(QwtScaleDiv(start, end, ticks)); // start is 500, end is 9500
    5. ui.timeSampleSlider->setRange(start, end);
    6. ui.timeSampleSlider->setScaleMaxMinor(0);
    7. ui.timeSampleSlider->setValue(start);
    8. ui.timeSampleSlider->setEnabled(true);
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    Build and run the application below ( unmodified - please no more modifications from your code ).

    Isn't this what you want ?

    Qt Code:
    1. #include <qapplication.h>
    2. #include <qwt_slider.h>
    3. #include <qwt_scale_div.h>
    4.  
    5. int main( int argc, char **argv )
    6. {
    7. QApplication a( argc, argv );
    8.  
    9. QwtSlider slider( NULL, Qt::Horizontal, QwtSlider::BottomScale );
    10.  
    11. QList< double > ticks[ QwtScaleDiv::NTickTypes ];
    12. ticks[ QwtScaleDiv::MajorTick ] << 500 << 1500 << 2500 << 3500
    13. << 4500 << 5500 << 6500 << 7500 << 8500 << 9500;
    14.  
    15. slider.setScale( QwtScaleDiv( 500, 9500, ticks ) );
    16. slider.setRange( 500, 9500, 1000 );
    17.  
    18. slider.show();
    19.  
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    This is really a "schwere Geburt",

    Uwe

  15. The following user says thank you to Uwe for this useful post:

    RomanRdgz (10th September 2012)

  16. #15
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    Ok, it does work now. The problematic line was:

    Qt Code:
    1. ui.timeSampleSlider->setScaleMaxMinor(0);
    To copy to clipboard, switch view to plain text mode 

    Which is weird, but without that line my code works exactly as yours. Thanks for your time!

  17. #16
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    One more question: about the method you gave me before to customize the labels, how could I use it?

    I am accessing the QwtSlider from the ui, with ui.timeSampleSlider, as you have already seen when I showed code here. I know I can get the ScaleDraw associated to my QwtSlider with the scaleDraw() method, but I don't know how to write the function you gave me then.

    I can think of:
    Qt Code:
    1. virtual QwtText ui.timeSampleSlider.scaleDraw()::label( double value ) const
    2. {
    3. return QwtScaleDraw::label( value / 1000.0 );
    4. }
    To copy to clipboard, switch view to plain text mode 

    which is obviously wrong, but I can't think of how to access to the ScaleDraw. By the way, I'm writting this method into my code, into the same cpp file I have been working before to set the ticks where I wanted.

    How can I use this method then? Thanks once more!

    Quote Originally Posted by Uwe View Post
    Then set the ticks to 500, 1000 and modify how the tick labels are represented:

    Qt Code:
    1. virtual QwtText YourScaleDraw::label( double value ) const
    2. {
    3. return QwtScaleDraw::label( value / 1000.0 );
    4. }
    To copy to clipboard, switch view to plain text mode 

    HTH,
    Uwe

  18. #17
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Customize tick labels in a QwtSlider

    Qt Code:
    1. class YourScaleDraw: public QwtScaleDraw
    2. {
    3. public:
    4. virtual QwtText label( double value ) const
    5. {
    6. return QwtScaleDraw::label( value / 1000.0 );
    7. }
    8. };
    9.  
    10. ui.timeSampleSlider->setScaleDraw( new YourScaleDraw() );
    To copy to clipboard, switch view to plain text mode 
    Assigning the YourScaleDraw object has to be done before you set the ticks !

    But please stay to Qwt related questions, when posting here. There are other forums dedicated to C++.

    Uwe

  19. The following user says thank you to Uwe for this useful post:

    RomanRdgz (10th September 2012)

Similar Threads

  1. How to hide tick labels
    By ken123 in forum Qwt
    Replies: 1
    Last Post: 24th November 2011, 08:15
  2. Y Axis tick labels cut off on left
    By rbergeron in forum Qwt
    Replies: 2
    Last Post: 4th November 2011, 15:29
  3. Axis Tick Labels Overlap
    By amoswood in forum Qwt
    Replies: 3
    Last Post: 8th June 2010, 16:52
  4. y-axis tick labels trimmed
    By gib in forum Qwt
    Replies: 2
    Last Post: 2nd April 2010, 06:19
  5. Tick Labels for Slider
    By jcraig in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 18:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.