Results 1 to 4 of 4

Thread: Accessing member variables of QwtPlotCurve from within the

  1. #1
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Accessing member variables of QwtPlotCurve from within the

    Accessing member variables (minXValue, minYValue...) of QwtPlotCurve from within the parent class
    Qwt 6.0.1, Qt 4.8.4

    Hello everyone!

    I have realized my own plot class derived from QwtPlot.
    I have defined a method setMarkerOnMax() which needs as parameters among others a pointer to the data and the data size. But those are already known by the plot object, so I find it boring to have to reinter that information.
    Within the method attachCurve() I can successfuly access those variables, but I did´nt succeed in accessing them from within the parent class. I took a look to Qwt_plot_item and Qwt_plot_dict but have no idea how to proceed.

    I have attached some code snippets.

    Any idea?

    Thanks for reading.

    Alain

    Qt Code:
    1. // Usage
    2. Plot2D* pLogSpectrum = new Plot2D( "Spectrum", "frequency", "magnitude [dB]", Qt::darkBlue, app );
    3. pLogSpectrum->attachCurve( pXData, pLogMag, HALF_LENGTH, "noisy sine", Qt::cyan );
    4. pLogSpectrum->setMarkerOnMax( pXData, pLogMag, HALF_LENGTH, -3 );
    To copy to clipboard, switch view to plain text mode 

    I would prefer:
    Qt Code:
    1. pLogSpectrum->setMarkerOnMax( -3 );
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // Declaration
    2. class Plot2D: public QwtPlot
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. Plot2D( QString plotTitle = "Data Plot",
    8. QString xLabel = "x-axis",
    9. QString yLabel = "y-axis",
    10. QColor backgoundColor = Qt::darkGray,
    11. QWidget *parent = NULL );
    12.  
    13. void attachCurve( double* xData,
    14. double *yData,
    15. int dataCount,
    16. QString curveLabel = "",
    17. QColor lineColor = Qt::yellow );
    18.  
    19. void setMarkerOnMax( SLData_t* xData,
    20. SLData_t* yData,
    21. SLArrayIndex_t dataCount,
    22. int precision = 4, // positive value for '%.*g', negative value for '%.*f'
    23. Qt::Alignment alignment = Qt::AlignHCenter | Qt::AlignTop );
    24.  
    25. void setMarker( double X,
    26. double Y,
    27. int precision = 4, // positive value for '%.*g', negative value for '%.*f'
    28. Qt::Alignment alignment = Qt::AlignHCenter | Qt::AlignTop );
    29.  
    30. ...
    31.  
    32. public Q_SLOTS:
    33. void autoRescale( QRectF );
    34. void printDocument();
    35. void exportDocument();
    36.  
    37. protected:
    38. virtual void resizeEvent( QResizeEvent * );
    39.  
    40. private:
    41. int mWindowHeight, mWindowWidth;
    42. int mWindowXPosition, mWindowYPosition;
    43. QwtPlotGrid* grid;
    44. QwtLegend* legend;
    45. QwtPlotZoomer* zoomer;
    46. QPushButton* btnPrint;
    47. QPushButton* btnExport;
    48. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // implementation
    2. Plot2D::Plot2D( QString plotTitle,
    3. QString xLabel,
    4. QString yLabel,
    5. QColor backgroundColor,
    6. QWidget *parent ):
    7. QwtPlot( parent )
    8. {
    9. // plot object
    10. setWindowTitle ( QString( "SigLibGraph - Plot2D" ));
    11. setTitle( plotTitle );
    12. setCanvasBackground( backgroundColor);
    13.  
    14. // window size and position
    15. setMinimumSize( QSize( 240, 170 ));
    16. mWindowWidth = 550; mWindowHeight = 365;
    17. mWindowXPosition = 200; mWindowYPosition = 200;
    18. setGeometry( mWindowXPosition, mWindowYPosition, mWindowWidth, mWindowHeight );
    19.  
    20. // axis
    21. setAxisTitle( QwtPlot::xBottom, xLabel );
    22. setAxisTitle( QwtPlot::yLeft, yLabel );
    23.  
    24. // grid
    25. grid = new QwtPlotGrid;
    26. grid->enableXMin( true );
    27. grid->setMajPen( QPen( Qt::white, 0, Qt::DotLine ));
    28. grid->setMinPen( QPen( Qt::gray, 0 , Qt::DotLine ));
    29. grid->attach( this );
    30.  
    31. // legend
    32. legend = new QwtLegend;
    33. insertLegend( legend, QwtPlot::RightLegend );
    34.  
    35. //zoomer
    36. ...
    37.  
    38. // print functionality
    39. btnPrint = new QPushButton( "&Print", this );
    40. ...
    41.  
    42. // export functionality
    43. btnExport = new QPushButton( "&Export", this );
    44. ...
    45.  
    46. show();
    47. }
    48.  
    49. void Plot2D::attachCurve( SLData_t* xData,
    50. SLData_t* yData,
    51. int dataCount,
    52. QString curveLabel,
    53. QColor lineColor )
    54. {
    55. QwtPlotCurve* curve = new QwtPlotCurve;
    56.  
    57. curve->setSamples( xData, yData, dataCount );
    58. // following works well
    59. //qDebug() << "xMin=" << curve->minXValue() << " | xMax=" << curve->maxXValue()
    60. // << " | yMin=" << curve->minYValue() << " | yMax=" << curve->maxYValue()
    61. // << " | data count=" << curve->dataSize();
    62.  
    63. QPen curvePen;
    64. curvePen.setColor( lineColor );
    65. curve->setPen( curvePen );
    66.  
    67. curve->setRenderHint( QwtPlotItem::RenderAntialiased );
    68.  
    69. curve->setTitle( curveLabel );
    70.  
    71. if ( curveLabel.isEmpty() )
    72. curve->setItemAttribute( QwtPlotItem::Legend, false );
    73. else
    74. curve->setLegendAttribute( QwtPlotCurve::LegendShowLine );
    75.  
    76. curve->attach( this );
    77. }
    78.  
    79. void Plot2D::setMarkerOnMax( SLData_t* xData,
    80. SLData_t* yData,
    81. SLArrayIndex_t dataCount,
    82. int precision, // positive value for '%.*g', negative value for '%.*f'
    83. Qt::Alignment alignment )
    84. {
    85. SLArrayIndex_t index = SDA_MaxPos( xData, dataCount ); // get the index of max value
    86. setMarker( xData[index], yData[index], precision, alignment ); // set marker position
    87. }
    88.  
    89. void Plot2D::setMarker( double X,
    90. double Y,
    91. int precision,
    92. Qt::Alignment alignment )
    93. {
    94. QwtPlotMarker* marker = new QwtPlotMarker();
    95. marker->setValue( X, Y );
    96. marker->setSymbol( new QwtSymbol( QwtSymbol::Cross,
    97. QColor( Qt::white ),
    98. QColor( Qt::white ),
    99. QSize( 10, 10 )));
    100. QString label;
    101. if (precision < 0)
    102. label.sprintf( "(%.*f | %.*f)", -precision, X, -precision, Y );
    103. else
    104. label.sprintf( "(%.*g | %.*g)", precision, X, precision, Y );
    105. QwtText text( label );
    106. text.setColor( QColor( Qt::white ));
    107. text.setFont( QFont("Helvetica", 8, QFont::Bold ));
    108.  
    109. marker->setLabel( text );
    110. marker->setLabelAlignment( alignment );
    111. marker->attach( this );
    112. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by alainstgt; 29th October 2013 at 14:32. Reason: updated contents

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

    Default Re: Accessing member variables of QwtPlotCurve from within the

    The plot has access for all plot items, where the curves are among them, each curve has access to its data object.
    But as the data object is only an abstract API for the curve ( doesn't depend on how data is stored in memory ! ) you have to do a cast for QwtPointArrayData - what is the type of API your code is using.

    Uwe

  3. #3
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing member variables of QwtPlotCurve from within the

    Uwe,

    I am not sure wether I understand your question about which type of API I am using.
    The data (xData and pLogMag) are 2 C-array allocated dynamically onto the heap.
    My problem is how to access to the curve (which is an instance of QwtCurvePlot) and its attributes from within the method setMarkerOnMax().
    In the method attachCurve() you can see how curve is instanciated and attached to the parent Plot2D.

    Alain

  4. #4
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing member variables of QwtPlotCurve from within the

    in the meanwhile I have solved the problem of accessing the data, and I could change my function
    Qt Code:
    1. setMarkerOnMax( double* xData, double* yData, int dataCount, int precision, Qt::Alignment alignment )
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. setMarkerOnMax( int precision, Qt::Alignment alignment )
    To copy to clipboard, switch view to plain text mode 
    This work well as long as I have only one curve attached to the plot.

    In the case I have several curve attached to the plot, and I don´t want to identify all maxima, but only that of one curve for example, I have to add an overloaded function
    Qt Code:
    1. setMarkerOnMax( double* yData, int precision, Qt::Alignment alignment )
    To copy to clipboard, switch view to plain text mode 
    yData beeing the pointer onto the data whose maximum has to be labeled.

    To do so, I have the problem of identifying which curve of the list belongs to the yData (C-array)

    As starting point, I have the list of all curves attached to the plot.

    One solution would be to calculate an hash of the data of each curve in the list and compare them with the hash of the data yData passed as argument - see code fragment below -.

    This will work but seems to me to be an overkill!

    Is there a better method?

    Alain

    Qt Code:
    1. void setMarkerOnMax( double* yData, int precision, Qt::Alignment alignment )
    2. {
    3. QwtPlotItemIterator it;
    4. const QwtPlotItemList& itemList = this->itemList();
    5. QwtPlotItemList curveList;
    6. // select the plotItem
    7. for ( it = itemList.begin(); it != itemList.end(); ++it ) {
    8. if ( (*it)->rtti() == QwtPlotItem::Rtti_PlotCurve ) {
    9. curveList += *it;
    10. }
    11. }
    12. // calculate an hash of each curve for identification
    13. for ( unsigned int i = 0; i < curveList.count(); ++i ) {
    14. QwtPlotCurve* curve = static_cast<QwtPlotCurve *>( curveList[i] );
    15. // compute some hash data for each curve
    16. }
    17. // compare the hash of each curve with hash of C-array yData passed as argument
    18. // to get the index of curveList corresponding to yData
    19. // label the curve with the right index
    20. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 15:53
  2. QDevelop debuggng - viewing class member variables
    By dbrmik in forum Qt-based Software
    Replies: 0
    Last Post: 7th January 2009, 11:40
  3. Replies: 22
    Last Post: 8th October 2008, 14:54
  4. Replies: 5
    Last Post: 18th December 2007, 12:39
  5. Accessing Environment Variables
    By mhoover in forum Qt Programming
    Replies: 6
    Last Post: 21st September 2006, 16:05

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.