Results 1 to 6 of 6

Thread: setting ticks on QwtPlotScaleItem

  1. #1
    Join Date
    Jul 2010
    Location
    France
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default setting ticks on QwtPlotScaleItem

    Hello,

    Firstly, thank you very much for Qwt, it is such a great tool !

    I am currently trying to implement a plot with the standard axisWidgets (left and bottom) having automatic ticks outside of the plot, and i need to have also one vertical scale and one horizontal scale inside the plot. For this I attached 2 QwtPlotScaleItem instances to the plot. I need to have the ticks at specific positions for these 2 scales, so I call QwtPlotScaleItem::setScaleDiv.The problem is when the plot is zoomed:the ticks coordinates do not seem to be updated, so it is not coherent any more with the graph and the axis widgets.
    What am I doing wrong ?

    before zooming:
    beforeZoom.jpg
    after zooming:
    afterZoom.jpg

    Thanks again

    Eric

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

    Default Re: setting ticks on QwtPlotScaleItem

    Using QwtPlotScaleItem::setScaleDiv sets up an individual scale that is unrelated to the coordinate system spanned by the outer axes - what is probably not what you want to do.

    So I guess you are looking for something like the following code:

    Qt Code:
    1. class ScaleItem: public QwtPlotScaleItem
    2. {
    3. public:
    4. ScaleItem():
    5. {
    6. }
    7.  
    8. protected:
    9. void updateScaleDiv( const QwtScaleDiv& xScaleDiv,
    10. const QwtScaleDiv& yScaleDiv )
    11. {
    12. QwtScaleDiv sd( yScaleDiv.lowerBound(), yScaleDiv.upperBound() );
    13. sd.setTicks( QwtScaleDiv::MajorTick, QList<double>() << 1 << 2 << ... );
    14.  
    15. QwtPlotScaleItem::updateScaleDiv( xScaleDiv, sd );
    16. }
    17. };
    To copy to clipboard, switch view to plain text mode 
    Uwe

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

    ericLemanissier (13th August 2014)

  4. #3
    Join Date
    Jul 2010
    Location
    France
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: setting ticks on QwtPlotScaleItem

    Thank you for your answer.

    I made a solution just a bit more generic, which works well:
    Qt Code:
    1. class ScaleItem: public QwtPlotScaleItem
    2. {
    3. public:
    4. ScaleItem(const QwtScaleDiv& scaleDiv, QwtScaleDraw::Alignment alignment = QwtScaleDraw::BottomScale,
    5. const double pos = 0.0 ):
    6. QwtPlotScaleItem( alignment, pos), m_scaleDiv(scaleDiv), m_alignment(alignment)
    7. {
    8. }
    9.  
    10. protected:
    11. void updateScaleDiv( const QwtScaleDiv& xScaleDiv,
    12. const QwtScaleDiv& yScaleDiv )
    13. {
    14. switch(m_alignment)
    15. {
    16. case QwtScaleDraw::BottomScale:
    17. case QwtScaleDraw::TopScale:
    18. m_scaleDiv.setInterval(xScaleDiv.interval());
    19. QwtPlotScaleItem::updateScaleDiv( m_scaleDiv, yScaleDiv );
    20. break;
    21. case QwtScaleDraw::LeftScale:
    22. case QwtScaleDraw::RightScale:
    23. m_scaleDiv.setInterval(yScaleDiv.interval());
    24. QwtPlotScaleItem::updateScaleDiv( xScaleDiv, m_scaleDiv);
    25. break;
    26. }
    27.  
    28. }
    29. private:
    30. QwtScaleDiv m_scaleDiv;
    31. const QwtScaleDraw::Alignment m_alignment;
    32. };
    To copy to clipboard, switch view to plain text mode 

    It gives the following expected result after zooming:
    afterzoom.jpg

    I know have a question about the labels of these scale. As you can see in the screenshot above, they are hardly reading because the overlap with the colored square. I am using QwtPlotScaleItem::scaleDraw()->setLabelAlignment(Qt::AlignBottom | Qt::AlignLeft); which improves the situation.
    However i need to shift a bit the labels (on the left for the horizontal scale, and to the bottom for the vertical scale). I tried the QwtPlotScaleItem::scaleDraw()->setSpacing(40) but it does not shift in the direction I need. Do i need to subclass QwtScaleDraw for this ?

    Again, many thanks !

    Eric

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

    Default Re: setting ticks on QwtPlotScaleItem

    Maybe setting a different alignment ( f.e labels left from the ticks for horizontal scales ) is what you need.

    Unfortunately the methods for calculating the geometry of the labels are not virtual and can't be manipulated easily.

    Uwe

  6. #5
    Join Date
    Jul 2010
    Location
    France
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: setting ticks on QwtPlotScaleItem

    What do you mean by different alignment ? I already tried QwtScaleDraw::setLabelAlignment(Qt::AlignLeft), and it does not allow to increase the shift between the label and the tick (the horizontal shift in this case).

    I found an interesting other way to handle this : I could subclass QwtTextEngine and override QwtTextEngine::textMargins( const QFont &font, const QString &text, double &left, double &right, double &top, double &bottom ), and then calling QwtText::setTextEngine to force the use of this textengine for PlainText. However this method is static, so it will also set the textEngine for all other QwtText, an it is not possible since I have other plots displayed at the same time which need to keep the standard behavior for their QwtText.
    Do you think there could be a possibility in this direction ?

    Eric

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

    Default Re: setting ticks on QwtPlotScaleItem

    You could overload QwtScaleDraw::label() and simply add a blank at the end of each text.

    If you want to continue with a modified text engine you could register it with a new format type ( QwtText::setTextEngine() ) and return the labels of QwtScaleDraw::label() using this specific format type.

    Uwe

Similar Threads

  1. Replies: 1
    Last Post: 25th June 2014, 18:46
  2. Replies: 6
    Last Post: 20th March 2014, 10:15
  3. Replies: 0
    Last Post: 26th September 2013, 15:28
  4. Replies: 14
    Last Post: 30th October 2012, 15:45
  5. QwtPlotScaleItem
    By beni46 in forum Qwt
    Replies: 1
    Last Post: 11th February 2011, 22:42

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.