Results 1 to 9 of 9

Thread: Qwt : QwtPlotZoomer with Qt 6.0.1, fixed grid

  1. #1
    Join Date
    Jun 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    4

    Default Qwt : QwtPlotZoomer with Qt 6.0.1, fixed grid

    Hello,

    I try to have a zoom function which doesn't change the grid display. Major ticks have to stay on the same position. If I use QwtPlotZoomer with function zoom(QRect) the display of the grid is different.
    So I have reimplement the QwtPlotZoomer::zoom(QRect) function and add this code :

    Qt Code:
    1. float spaceX = (float)pRect.width()/5;
    2. float spaceY = (float)pRect.height()/10;
    3.  
    4. QList<double> majorTicksX;
    5. QList<double> majorTicksY;
    6. for(int i=0; i<11; i++)
    7. {
    8. if(i<6)
    9. majorTicksX.append(pRect.x() + spaceX*i);
    10.  
    11. majorTicksY.append(pRect.y() + spaceY*i);
    12. }
    13.  
    14. divX.setInterval(double(pRect.x()), double(pRect.x() + spaceX*5));
    15. divX.setTicks(QwtScaleDiv::MajorTick, majorTicksX);
    16. divY.setInterval(double(pRect.y()), double(pRect.y() + spaceY*10));
    17. divY.setTicks(QwtScaleDiv::MajorTick, majorTicksY);
    18.  
    19. plot()->setAxisScaleDiv(QwtPlot::xBottom, divX);
    20. plot()->setAxisScaleDiv(QwtPlot::yLeft, divY);
    21.  
    22. static_cast<YourScaleDraw*>(plot()->axisScaleDraw(QwtPlot::xBottom))->min = pRect.x();
    23. static_cast<YourScaleDraw*>(plot()->axisScaleDraw(QwtPlot::xBottom))->max = pRect.x()+ spaceX*5;
    24.  
    25. plot()->replot();
    To copy to clipboard, switch view to plain text mode 

    Class YourScaleDraw
    Qt Code:
    1. class YourScaleDraw: public QwtScaleDraw
    2. {
    3. public:
    4. virtual QwtText label( double v ) const
    5. {
    6. if(alignment() == LeftScale)
    7. {
    8. return QwtScaleDraw::label( int( v ) );
    9. }
    10. else if(alignment() == BottomScale)
    11. {
    12. return QwtScaleDraw::label(v);
    13. }
    14. }
    15.  
    16. virtual void drawLabel(QPainter* painter, double value) const
    17. {
    18. if(value == min && alignment() == BottomScale)
    19. {
    20. QRect boundingRect = boundingLabelRect(painter->font(), value);
    21. boundingRect.setX(boundingRect.x() + boundingRect.width()/2);
    22. boundingRect.setWidth(boundingRect.width()*2);
    23. painter->fillRect(boundingRect, QColor(Qt::darkRed));
    24. painter->drawText(boundingRect, Qt::AlignLeft, label(value).text());
    25. }
    26. else if(value == max && alignment() == BottomScale)
    27. {
    28. QRect boundingRect = boundingLabelRect(painter->font(), value);
    29. boundingRect.setX(boundingRect.x() - boundingRect.width()/2);
    30. boundingRect.setWidth(boundingRect.width() - boundingRect.width()/2);
    31. painter->fillRect(boundingRect, QColor(Qt::darkRed));
    32. painter->drawText(boundingRect, Qt::AlignLeft, label(value).text());
    33. }
    34. else
    35. {
    36. QRect boundingRect = boundingLabelRect(painter->font(), value);
    37. painter->fillRect(boundingRect, QColor(Qt::darkRed));
    38. painter->drawText(boundingRect, Qt::AlignLeft, label(value).text());
    39. }
    40. }
    41.  
    42. double min, max;
    43. };
    To copy to clipboard, switch view to plain text mode 

    But this is not working with Qwt 6.0.1 but it is working with Qwt 6.1.0.

    What can I do to make it work with Qwt 6.0.1.

    Thanks
    Lionel

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

    Default Re: Qwt : QwtPlotZoomer with Qt 6.0.1, fixed grid

    What about disabling QwtPlotItem::ScaleInterest for your grid item - at least once you have your initial setup ?

    Uwe

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

    lionel.s (7th July 2015)

  4. #3
    Join Date
    Jun 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    4

    Default Re: Qwt : QwtPlotZoomer with Qt 6.0.1, fixed grid

    It is an Histogram and I'm drawning data with a reimpl of drawColumn from QwtPlotHistogram.

    I'm trying to zoom -> change the x and y axis so the grid doesn't change. I always need 5 major ticks on the bottom axis and 10 on the left axis. But they should not move. Only the value of each ticks may change. Well it works with the Qwt 6.1.0 but the customer don't want to use another version than the 6.0.1. I don't use QwtPlotItem.

    Is it possible with Qwt 6.0.1 to have a fixe grid ?

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

    Default Re: Qwt : QwtPlotZoomer with Qt 6.0.1, fixed grid

    Instead of overloading QwtPlotZoomer::zoom() better overload QwtLinearScaleEngine::divideScale() - this is the right place for your first code snippet !
    The motivation behind the second seems to be to right/left align the min/max tick label, to avoid the extra space at the borders ?

    Which of your code snippets doesn't work with Qwt 6.0 ?

    Uwe

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

    lionel.s (7th July 2015)

  7. #5
    Join Date
    Jun 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    4

    Default Re: Qwt : QwtPlotZoomer with Qt 6.0.1, fixed grid

    Thanks for your answer.

    Yes the class YourScaleDraw is used to avoid extra space at the border which is not working very well but it is near the good solution !

    Concerning QwtLinearScaleEngine, I'll have a look.

    In fact you did a great job with Qwt, thanks. It is quite hard to understand everything but it is very good.


    Added after 1 43 minutes:


    Ok I understand how QwtLinearScaleEngine divideScale works.

    But how do I set QwtLinearScaleEngine, on which object ?

    Actualy if I used it in the Zoom function of the QwtPlotZoomer it still do nothing.
    Last edited by lionel.s; 7th July 2015 at 14:09.

  8. #6
    Join Date
    Jun 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    4

    Default Re: Qwt : QwtPlotZoomer with Qt 6.0.1, fixed grid

    Ok it is working.

    But the last value is not displayed. Does anyone know how to do it ?

    Here is my code :
    Qt Code:
    1. QwtScaleDiv divideScale(double x1, double x2, int numMajorSteps, int numMinorSteps, double stepSize = 0.0) const
    2. {
    3. double step = (x2 - x1)/(numMajorSteps);
    4.  
    5. QList<double> Ticks[QwtScaleDiv::NTickTypes];
    6.  
    7. for (unsigned int i = 0; i < numMajorSteps; i++) {
    8. Ticks[QwtScaleDiv::MajorTick].append(x1 + i*step);
    9. }
    10.  
    11. return QwtScaleDiv(QwtInterval(x1, x1 + step*numMajorSteps), Ticks);
    12. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Qwt : QwtPlotZoomer with Qt 6.0.1, fixed grid

    numMajorSteps results in numMajorSteps + 1 major ticks !

    Uwe

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

    lionel.s (8th July 2015)

  11. #8
    Join Date
    Jun 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    4

    Default Re: Qwt : QwtPlotZoomer with Qt 6.0.1, fixed grid

    Here is the complete solution :

    Qt Code:
    1. CustomZoomPlot::CustomZoomPlot(QwtPlotCanvas *pCanvas, bool doReplot)
    2. : QwtPlotZoomer(pCanvas, doReplot)
    3. {
    4. }
    5.  
    6. CustomZoomPlot::~CustomZoomPlot()
    7. {
    8. }
    9.  
    10. void CustomZoomPlot::unZoom()
    11. {
    12. // My default scale
    13. zoom(QRectF(0, 0, 100, 100));
    14. }
    15.  
    16. void CustomZoomPlot::widgetMouseReleaseEvent(QMouseEvent *pEvt)
    17. {
    18. if(pEvt->button() == Qt::RightButton)
    19. {
    20. unZoom();
    21. }
    22. else
    23. {
    24. QwtPlotZoomer::widgetMouseReleaseEvent(pEvt);
    25. }
    26. }
    27.  
    28. void CustomZoomPlot::zoom(const QRectF &pRect)
    29. {
    30. CustomScaleEngine *customSE = new CustomScaleEngine();
    31.  
    32. div = customSE->divideScale(pRect.x(), pRect.x() + pRect.width(), 5, 0);
    33. plot()->setAxisScaleDiv(QwtPlot::xBottom, div);
    34.  
    35. static_cast<YourScaleDraw*>(plot()->axisScaleDraw(QwtPlot::xBottom))->min = div.lowerBound();
    36. static_cast<YourScaleDraw*>(plot()->axisScaleDraw(QwtPlot::xBottom))->max = div.upperBound();
    37.  
    38. div = customSE->divideScale(pRect.y(), pRect.y() + pRect.height(), 10, 0);
    39. plot()->setAxisScaleDiv(QwtPlot::yLeft, div);
    40.  
    41.  
    42. plot()->replot();
    43. }
    44.  
    45. void CustomZoomPlot::drawRubberBand(QPainter* painter) const
    46. {
    47. if (dynamic_cast<const QBitmap*>(painter->device()))
    48. {
    49. painter->setBrush(Qt::color1);
    50. }
    51. else
    52. {
    53. // Alpha
    54. QColor colorToApply = Qt::red;
    55. colorToApply.setAlpha(128);
    56. painter->setBrush(colorToApply);
    57. }
    58.  
    59. QwtPlotZoomer::drawRubberBand( painter );
    60. }
    To copy to clipboard, switch view to plain text mode 

    YourScaleDraw is a QwtScaleDraw and it is used to draw label in order to avoid space at the border.

    Thanks Uwe

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

    Default Re: Qwt : QwtPlotZoomer with Qt 6.0.1, fixed grid

    Well, your code is not yet exactly how it is supposed to be:

    a) unZoom()

    Better use zoom(0) to unzoom to the top of the zoom stack. Your code pushes [0, 0, 100, 100] on top instead.
    There is also no need for overloading widgetMouseReleaseEvent at all: better reconfigure the mouse patterns
    ( MouseSelect3, MouseSelect6, MouseSelect2 ) dealing with navigating on the zoom stack.

    Before creating the zoomer you would have to call:

    plot->setAxisScale( QwtPlot::yLeft, 0.0, 100.0 );
    plot->setAxisScale( QwtPlot::xBottom, 0.0, 100.0 );

    - so that the base of your zoom stack gets initialized properly.

    b) zoom

    Beside the application sets the scale explicitly with setAxisScaleDiv() the tick calculation is always done by QwtAbstractScaleEngine::divideScale().
    As in your case you always want to have those equidistant 5/10 ticks ( not only, when beeing in some zoom state ) the better code would be to assign the scale engines once at the beginning:

    plot->setAxisScaleEngine( QwtPlot::yLeft, new CustomScaleEngine );
    plot->setAxisScaleEmgine( QwtPlot::xBottom, new CustomScaleEngine );

    and then remove your zoom method completely.

    Also note, that the code dealing with a QBitmap in drawRubberband is Qwt <= 6.0 related. Since Qwt 6.1 the rubberband is a QwtWidgetOverlay, that calculates its mask in a different way.

    Uwe

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

    lionel.s (15th July 2015)

Similar Threads

  1. Replies: 0
    Last Post: 9th March 2015, 12:47
  2. Replies: 2
    Last Post: 4th September 2014, 11:09
  3. Replies: 1
    Last Post: 5th March 2012, 06:34
  4. QwtPlotZoomer example
    By ken123 in forum Qwt
    Replies: 2
    Last Post: 20th October 2011, 15:20
  5. Replies: 1
    Last Post: 19th July 2010, 07: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
  •  
Qt is a trademark of The Qt Company.