Results 1 to 5 of 5

Thread: painting from -0.5 value

  1. #1
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Arrow painting from -0.5 value

    I try to paint a spectrogram from QVector data.
    I expect that will draw from x-zero... but result on the screenshot:

    spectrogramma_0.5.png

    here is my code:

    Qt Code:
    1. // plot.h
    2.  
    3. #include <qwt_plot.h>
    4. #include <qwt_plot_spectrogram.h>
    5.  
    6. class SpectrogramData;
    7. class SpectrogramDataFile;
    8.  
    9. class Plot: public QwtPlot {
    10. Q_OBJECT
    11.  
    12. public:
    13. Plot( QWidget * = NULL );
    14.  
    15. public Q_SLOTS:
    16. void showSpectrogram( bool on );
    17.  
    18. void addColumn(const QVector<int> &col);
    19.  
    20.  
    21. private:
    22. QwtPlotSpectrogram *d_spectrogram;
    23. SpectrogramData *sData;
    24. SpectrogramDataFile *sDataFile;
    25. QwtPlotZoomer* zoomer;
    26. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // plot.cpp
    2.  
    3. #include <QFile>
    4. #include <QDebug>
    5. #include <qwt_raster_data.h>
    6. #include <qprinter.h>
    7. #include <qprintdialog.h>
    8. #include <qwt_color_map.h>
    9. #include <qwt_plot_spectrogram.h>
    10. #include <qwt_scale_widget.h>
    11. #include <qwt_scale_draw.h>
    12. #include <qwt_plot_zoomer.h>
    13. #include <qwt_plot_panner.h>
    14. #include <qwt_plot_layout.h>
    15. #include <qwt_plot_renderer.h>
    16. #include "plot.h"
    17. #include "SpectrogramData.h"
    18. #include "CONST.h"
    19.  
    20. const qreal minX = -1.0; // if it will 0.0 we have the same result, just cut axis
    21.  
    22. //------------------------------------------------------------------------------------------
    23. class MyZoomer: public QwtPlotZoomer {
    24. public:
    25. MyZoomer( QwtPlotCanvas *canvas ):
    26. QwtPlotZoomer( canvas ) {
    27. setTrackerMode( AlwaysOn );
    28. }
    29.  
    30. virtual QwtText trackerTextF( const QPointF &pos ) const {
    31. QColor bg( Qt::white );
    32. bg.setAlpha( 200 );
    33.  
    34. QwtText text = QwtPlotZoomer::trackerTextF( pos );
    35. text.setBackgroundBrush( QBrush( bg ) );
    36. return text;
    37. }
    38. };
    39.  
    40. //------------------------------------------------------------------------------------------
    41. class SpectrogramData: public QwtRasterData {
    42.  
    43. private:
    44. int maxX;
    45. int maxY;
    46.  
    47. vecArray column;
    48. Matrix detectionMatrix;
    49.  
    50. public:
    51. SpectrogramData() {
    52. maxX = 0;
    53. maxY = 359;
    54.  
    55. setInterval(Qt::XAxis, QwtInterval(minX, maxX));
    56. setInterval(Qt::YAxis, QwtInterval(minX, maxY));
    57. setInterval(Qt::ZAxis, QwtInterval(minX, 10.0));
    58. }
    59.  
    60. void setMaxXIncremet() {
    61. ++maxX;
    62. setInterval(Qt::XAxis, QwtInterval(minX, maxX));
    63. }
    64.  
    65. void setMaxX(const int val) {
    66. setInterval(Qt::XAxis, QwtInterval(minX, val));
    67. }
    68.  
    69. void setMaxY(const int val) {
    70. setInterval(Qt::YAxis, QwtInterval(0.0, val));
    71. }
    72.  
    73. void addColumn(const QVector<int> &col) {
    74. detectionMatrix.push_back(col);
    75. }
    76.  
    77. virtual double value(double x, double y) const {
    78. int ix = qRound(x);
    79. int iy = qRound(y);
    80.  
    81. if ( (ix >= 0) && (ix < maxX) && (iy >= 0) && (iy < maxY) ) {
    82. // qDebug() << x;
    83. return detectionMatrix[ix][iy];
    84. }
    85. return 0;
    86. }
    87. };
    88.  
    89. //------------------------------------------------------------------------------------------
    90. class ColorMap: public QwtLinearColorMap {
    91. public:
    92. ColorMap():
    93. QwtLinearColorMap( Qt::darkCyan, Qt::red ) {
    94. addColorStop( 0.6, Qt::cyan );
    95. // addColorStop( 0.6, Qt::green );
    96. addColorStop( 0.9, Qt::yellow );
    97. }
    98. };
    99.  
    100. //------------------------------------------------------------------------------------------
    101. Plot::Plot( QWidget *parent ):
    102. QwtPlot( parent ) {
    103.  
    104. sData = new SpectrogramData();
    105. sDataFile = new SpectrogramDataFile();
    106. d_spectrogram = new QwtPlotSpectrogram();
    107. d_spectrogram->setRenderThreadCount( 0 ); // use system specific thread count
    108.  
    109. d_spectrogram->setColorMap(new ColorMap());
    110.  
    111. d_spectrogram->setData( sData );
    112. d_spectrogram->attach( this );
    113.  
    114. const QwtInterval zInterval = d_spectrogram->data()->interval( Qt::ZAxis );
    115. QwtScaleWidget *rightAxis = axisWidget(QwtPlot::yRight); // A color bar on the right axis
    116. rightAxis->setTitle(tr("Amplitude"));
    117. rightAxis->setColorBarEnabled(true);
    118. rightAxis->setColorMap(zInterval, new ColorMap());
    119.  
    120. setAxisScale( QwtPlot::yRight, zInterval.minValue(), zInterval.maxValue() );
    121. enableAxis( QwtPlot::yRight );
    122.  
    123. plotLayout()->setAlignCanvasToScales(true);
    124. replot();
    125.  
    126. zoomer = new MyZoomer(canvas());
    127. zoomer->setMousePattern( QwtEventPattern::MouseSelect2,
    128. Qt::RightButton, Qt::ControlModifier );
    129. zoomer->setMousePattern( QwtEventPattern::MouseSelect3,
    130. /*Qt::Key_Delete*/Qt::RightButton );
    131.  
    132. QwtPlotPanner *panner = new QwtPlotPanner(canvas());
    133. panner->setAxisEnabled(QwtPlot::yRight, false);
    134. panner->setMouseButton(Qt::MidButton);
    135. panner->setMouseTracking(true);
    136.  
    137.  
    138. const QFontMetrics fm( axisWidget( QwtPlot::yLeft )->font() );
    139. QwtScaleDraw *sd = axisScaleDraw( QwtPlot::yLeft );
    140. sd->setMinimumExtent( fm.width( "100.00" ) );
    141.  
    142. const QColor c(Qt::darkBlue);
    143. zoomer->setRubberBandPen(c);
    144. zoomer->setTrackerPen(c);
    145. }
    146.  
    147.  
    148. //------------------------------------------------------------------------------------------
    149. void Plot::addColumn(const QVector<int> &col) {
    150. sData->addColumn(col);
    151. sData->setMaxXIncremet();
    152. const QwtInterval xInterval = d_spectrogram->data()->interval(Qt::XAxis);
    153. const QwtInterval yInterval = d_spectrogram->data()->interval(Qt::YAxis);
    154.  
    155. QRect rectTmp(QPoint(0, 0), QSize(xInterval.maxValue(), yInterval.maxValue()));
    156. zoomer->setZoomBase(rectTmp);
    157. replot();
    158. }
    159.  
    160. //------------------------------------------------------------------------------------------
    161. void Plot::showSpectrogram( bool on ) {
    162. d_spectrogram->setDisplayMode( QwtPlotSpectrogram::ImageMode, on );
    163. d_spectrogram->setDefaultContourPen( on ? QPen() : QPen( Qt::NoPen ) );
    164. replot();
    165. }
    To copy to clipboard, switch view to plain text mode 

    fill vector:
    Qt Code:
    1. QVector<int> col;
    2.  
    3. for(int i = 0; i != 360; i++) {
    4. addColumn.push_back(randInt(1, 10));
    5. }
    To copy to clipboard, switch view to plain text mode 

    so why is it painting from -0.5 would you explain?

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

    Default Re: painting from -0.5 value

    Quote Originally Posted by layer View Post
    so why is it painting from -0.5 would you explain?
    Your raster data object claims to have values for any position ( = you didn't set valid intervals with QwtRasterData::setInterval() for X/Y axes ).
    The reason why you get a different color starting at > -0.5 is because your SpectrogramData::value() implementation rounds to 0 returning a value from your matrix - instead of the default 0.

    As your code doesn't do much beside returning values from a value matrix I recommend to use QwtMatrixRasterData. Check the rasterview example.

    Uwe

  3. #3
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: painting from -0.5 value

    Quote Originally Posted by Uwe View Post
    Your raster data object claims to have values for any position ( = you didn't set valid intervals with QwtRasterData::setInterval() for X/Y axes ).
    The reason why you get a different color starting at > -0.5 is because your SpectrogramData::value() implementation rounds to 0 returning a value from your matrix - instead of the default 0.

    As your code doesn't do much beside returning values from a value matrix I recommend to use QwtMatrixRasterData. Check the rasterview example.

    Uwe
    a checked rasterview example, it's easier to use in my case, you're certainly right.
    also I read many threads about Qwt, thank you Uwe for wonderful Lib.

    but smth in this situation (with this example) I didn't understand exactly but I want

    so as you can see in the code I use setInterval() for X, it's not enough?

    step by step:
    Qt Code:
    1. addColumn(col); // push QVector<int> in QVector<QVector<int>>
    To copy to clipboard, switch view to plain text mode 

    then
    Qt Code:
    1. //setMaxXIncremet();
    2. ++maxX;
    3. setInterval(Qt::XAxis, QwtInterval(0.0, maxX)); // here I try to change Interval on X
    To copy to clipboard, switch view to plain text mode 

    and then
    Qt Code:
    1. replot();
    To copy to clipboard, switch view to plain text mode 

    You said that
    is because your SpectrogramData::value() implementation rounds to 0 returning a value from your matrix - instead of the default 0.
    am I right it's about this code?
    Qt Code:
    1. int ix = qRound(x);
    2. int iy = qRound(y);
    To copy to clipboard, switch view to plain text mode 

    Uwe, can't make out what I have to change to get the result I need (painting from 0)?

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

    Default Re: painting from -0.5 value

    Don't know about the code snippets from your second posting, but in the original code minX is set to -1.0 used in setInterval( XAxis, minX, ...). So here you indicate that you have valid values starting at -1,0.

    You didn't implement pixelHint() what indicates, that you can return useful values for any resolution ( of course you could implement a valid pixelHint(), what might increase performance significantly depending on the ratio between screen resolution and hint ).

    In your value() method you are rounding to the next integer, so f.e -0.499 is rounded to 0.. That's why you see the values from your first column displayed in the range ]-0.5, 0.5] on the X-axis.

    But again: use QwtMatrixRasterData. instead of reinventing the wheel.

    Uwe

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

    layer (12th December 2012)

  6. #5
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: painting from -0.5 value

    Quote Originally Posted by Uwe View Post
    use QwtMatrixRasterData. instead of reinventing the wheel.
    Uwe
    I agree, thanks a lot again

Similar Threads

  1. painting in QwtPlotGrid
    By ready in forum Qwt
    Replies: 2
    Last Post: 18th July 2011, 06:26
  2. Regarding Painting
    By archanasubodh in forum General Programming
    Replies: 6
    Last Post: 8th August 2008, 18:23
  3. Painting in QCanvasView
    By JimBrown in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2007, 22:29
  4. Painting TreeWidget
    By VireX in forum Qt Programming
    Replies: 9
    Last Post: 9th May 2007, 08:36
  5. About painting
    By Pang in forum Qt Programming
    Replies: 3
    Last Post: 28th March 2007, 17: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.