Results 1 to 3 of 3

Thread: Remove QwtPlotPicker from a QwtPlot canvas

  1. #1
    Join Date
    Feb 2016
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Remove QwtPlotPicker from a QwtPlot canvas

    Hello,

    I'm using the class "curvetracker" from the Qwt playground with some minor modifications:

    Qt Code:
    1. #include "curvetracker.h"
    2. #include <qwt_picker_machine.h>
    3. #include <qwt_series_data.h>
    4. #include <qwt_plot.h>
    5. #include <qwt_plot_curve.h>
    6.  
    7. struct compareX
    8. {
    9. inline bool operator()( const double x, const QPointF &pos ) const
    10. {
    11. return ( x < pos.x() );
    12. }
    13. };
    14.  
    15. CurveTracker::CurveTracker( QWidget *canvas ):
    16. QwtPlotPicker( canvas )
    17. {
    18. setTrackerMode( QwtPlotPicker::ActiveOnly );
    19. setRubberBand( VLineRubberBand );
    20.  
    21. setStateMachine( new QwtPickerDragPointMachine() );
    22. }
    23.  
    24. QRect CurveTracker::trackerRect( const QFont &font ) const
    25. {
    26. QRect r = QwtPlotPicker::trackerRect( font );
    27.  
    28. // Align the rect to the first curve
    29. const QwtPlotItemList curves = plot()->itemList( QwtPlotItem::Rtti_PlotCurve );
    30. if ( curves.size() > 0 )
    31. {
    32. QPointF pos = invTransform( trackerPosition() );
    33.  
    34. const QLineF line = curveLineAt(
    35. static_cast<const QwtPlotCurve *>( curves[0] ), pos.x() );
    36. if ( !line.isNull() )
    37. {
    38. const double curveY = line.pointAt(
    39. ( pos.x() - line.p1().x() ) / line.dx() ).y();
    40.  
    41. pos.setY( curveY );
    42. pos = transform( pos );
    43.  
    44. r.moveBottom( pos.y() );
    45. }
    46. }
    47.  
    48. return r;
    49. }
    50.  
    51. QwtText CurveTracker::trackerTextF( const QPointF &pos ) const
    52. {
    53. QwtText trackerText;
    54.  
    55. trackerText.setColor( Qt::white );
    56.  
    57. // Background color of the rectangle
    58. //QColor c( "#5e5e5e" );
    59. QColor c( "#858585" );
    60. trackerText.setBorderPen( QPen( c, 2 ) );
    61. c.setAlpha( 200 );
    62. trackerText.setBackgroundBrush( c );
    63.  
    64. QString info;
    65.  
    66. const QwtPlotItemList curves =
    67. plot()->itemList( QwtPlotItem::Rtti_PlotCurve );
    68.  
    69. for ( int i = 0; i < 1; i++ )
    70. {
    71. const QString curveInfo = curveInfoAt(
    72. static_cast<const QwtPlotCurve *>( curves[i] ), pos );
    73.  
    74. if ( !curveInfo.isEmpty() )
    75. {
    76. if ( !info.isEmpty() )
    77. info += "<br>";
    78.  
    79. info += curveInfo;
    80. }
    81. }
    82.  
    83. trackerText.setText( info );
    84. return trackerText;
    85. }
    86.  
    87. QString CurveTracker::curveInfoAt(
    88. const QwtPlotCurve *curve, const QPointF &pos ) const
    89. {
    90. const QLineF line = curveLineAt( curve, pos.x() );
    91. if ( line.isNull() )
    92. return QString::null;
    93.  
    94. int x = line.pointAt(
    95. ( pos.x() - line.p1().x() ) / line.dx() ).x();
    96. double y = line.pointAt(
    97. ( pos.x() - line.p1().x() ) / line.dx() ).y();
    98.  
    99. // Info "window" args: color, x and y.
    100. QString info( "<font color=""%1"">x : %2<br>y : %3</font>" );
    101. return info.arg( "white" ).arg( x ).arg( QString::number(y, 'f', 3) ); // curve->pen().color().name()
    102. }
    103.  
    104. QLineF CurveTracker::curveLineAt(
    105. const QwtPlotCurve *curve, double x ) const
    106. {
    107. QLineF line;
    108.  
    109. if ( curve->dataSize() >= 2 )
    110. {
    111. const QRectF br = curve->boundingRect();
    112. if ( br.isValid() && x >= br.left() && x <= br.right() )
    113. {
    114. int index = qwtUpperSampleIndex<QPointF>(
    115. *curve->data(), x, compareX() );
    116.  
    117. if ( index == -1 &&
    118. x == curve->sample( curve->dataSize() - 1 ).x() )
    119. {
    120. // the last sample is excluded from qwtUpperSampleIndex
    121. index = curve->dataSize() - 1;
    122. }
    123.  
    124. if ( index > 0 )
    125. {
    126. line.setP1( curve->sample( index - 1 ) );
    127. line.setP2( curve->sample( index ) );
    128. }
    129. }
    130. }
    131.  
    132. return line;
    133. }
    To copy to clipboard, switch view to plain text mode 


    In my plot class, I am using it like this:

    Qt Code:
    1. // dataPlot_ = QwtPlot
    2. CurveTracker* tracker = new CurveTracker( dataPlot_->canvas() );
    3. tracker->setStateMachine( new QwtPickerTrackerMachine() );
    4. tracker->setRubberBandPen( QPen(Qt::black, 1) );
    To copy to clipboard, switch view to plain text mode 

    Ok, it is working, but now I need to clean/reset my entire QwtPlot object.

    I'm using the following command to do so:

    Qt Code:
    1. dataPlot_->detachItems(QwtPlotItem::Rtti_PlotItem, true);
    To copy to clipboard, switch view to plain text mode 

    but when I move the mouse over my QwtPlot my application break. Investigating the problem I discovered that it is crashing because of the CurveTracker.

    How can I remove/delete the CurveTracker (QwtPlotPicker) from my dataPlot_ (QwtPlot) canvas?

    Thanks


    Added after 1 22 minutes:


    I solved the problem by disabling my QwtPlotPicker:

    Qt Code:
    1. tracker->setEnabled(false);
    To copy to clipboard, switch view to plain text mode 

    I don't know if it is the best/right solution, but it worked.

    Anyway, if anyone knows a better way to do so please let me know.
    Last edited by KelvinSP; 20th July 2016 at 22:32.

  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: Remove QwtPlotPicker from a QwtPlot canvas

    If you don't need it anymore:

    Qt Code:
    1. delete tracker;
    To copy to clipboard, switch view to plain text mode 
    Uwe

  3. #3
    Join Date
    Feb 2016
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Remove QwtPlotPicker from a QwtPlot canvas

    Quote Originally Posted by Uwe View Post
    If you don't need it anymore:

    Qt Code:
    1. delete tracker;
    To copy to clipboard, switch view to plain text mode 
    Uwe
    Thanks a lot Uwe, it worked.

Similar Threads

  1. Replies: 3
    Last Post: 22nd October 2013, 10:57
  2. Set size of QwtPlot/canvas
    By Hudo in forum Qwt
    Replies: 0
    Last Post: 23rd August 2012, 20:10
  3. QwtPlot/Canvas and QPainter
    By carhun in forum Qwt
    Replies: 1
    Last Post: 20th August 2012, 14:14
  4. Replies: 2
    Last Post: 2nd May 2012, 10:49
  5. Replies: 14
    Last Post: 20th April 2010, 12:40

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.