Results 1 to 18 of 18

Thread: mouse tracking showing float numbers

  1. #1
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default mouse tracking showing float numbers

    I have a 2d data to plot.
    The x-values range from 0 to 2000
    The y-values range 2e-11 to 4e-9
    The mouse tracker works perfectly for the x-values but it shows always 0.000 for the y-values. Is there a way to show float precision values on the mouse tracker?

    PS: I have done an experiment with both x and y values in the range of e-11 and the mouse tracker shows always 0.000 for both values

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mouse tracking showing float numbers

    write your own picker and overload the virtual "trackerText()"-method. Then you can define the number format which is used in the string (see QString::number())

    here is an example from my code. I'm using it in a spectrogram to show not only the position but also the value under the cursor. Also I want to have a semi-alpha background (otherwise the text would sometimes be hard to read)
    Qt Code:
    1. QwtText PlotPicker::trackerText (const QwtDoublePoint & pos) const
    2. {
    3. QwtText text("(" + QString::number(pos.x()) + "," + QString::number(pos.y()) + ") " + QString::number(m_spectrogram.ValueAt(pos.x(), pos.y())));
    4. QColor bgColor(Qt::black);
    5. bgColor.setAlpha(160);
    6. text.setBackgroundBrush(QBrush(bgColor));
    7. return text;
    8. }
    To copy to clipboard, switch view to plain text mode 

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

    fatecasino (17th January 2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse tracking showing float numbers

    how do you implement this in your code? I am based on the bode example, so I have a class for 2d plots called my2dPlot.

    Qt Code:
    1. QwtPlot *myPlot;
    2. QwtPlotGrid *grid;
    3.  
    4. QwtPlotCurve *mainCurve;
    5. QwtPlotMarker *mainCurveMarker;
    6.  
    7. QwtPlotCurve *continuumCurve;
    8. QwtPlotMarker *continuumCurveMarker;
    9. QwtPlotMarker *myMarker;
    10.  
    11. QwtPlotZoomer *m_zoomer;
    12. QwtPlotPicker *d_picker;
    13. QwtPlotPanner *d_panner;
    14.  
    15. QwtDoubleRect workingRect;
    To copy to clipboard, switch view to plain text mode 

    I suppose I have to include the new function in the header file.


    Qt Code:
    1. QwtText trackerText (const QwtDoublePoint & pos) const;
    To copy to clipboard, switch view to plain text mode 

    Then adjust your code in the .cpp file
    Qt Code:
    1. QwtText my2dPlot::trackerText (const QwtDoublePoint & pos) const
    2. {
    3. QwtText text("(" + QString::number(pos.x()) + "," + QString::number(pos.y()) + ") " + QString::number(myPlot.ValueAt(pos.x(), pos.y())));
    4. QColor bgColor(Qt::black);
    5. bgColor.setAlpha(160);
    6. text.setBackgroundBrush(QBrush(bgColor));
    7. return text;
    8. }
    To copy to clipboard, switch view to plain text mode 

    in the my2dPlot constructor I used to have a common picker/texttracker
    Qt Code:
    1. d_picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
    2. QwtPlotPicker::PolygonRubberBand, QwtPicker::AlwaysOn,
    3. myPlot->canvas());
    4. d_picker->setStateMachine(new QwtPickerPolygonMachine());
    5. d_picker->setRubberBandPen(QColor(Qt::green));
    6. d_picker->setTrackerPen(QColor(Qt::blue));
    7. d_picker->setTrackerFont(f);
    To copy to clipboard, switch view to plain text mode 

    How can i apply the new trackerText in the constructor?

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,060
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: mouse tracking showing float numbers

    What FelixB means is that you have to write your -own- picker class (derived from QwtPlotPicker) and reimplement the trackerText method. You then use an instance of your new picker class -instead- of the standard QwtPlotPicker.

    Notice that in the example FelixB gave, trackerText is not a member of the QwtPlot class, it is a member of his PlotPicker class.

    The code you posted is simply wrong. There -is- no trackerText in the QwtPlot class, so writing a new one in a derived class will do absolutely nothing since nothing ever calls it.

  6. #5
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse tracking showing float numbers

    I think I managed to create my own class myPicker.


    Qt Code:
    1. class myPicker : public QwtPlotPicker
    2. {
    3. public:
    4.  
    5. myPicker(QwtPlot::Axis xAxis, QwtPlot::Axis yAxis, QwtPicker::RubberBand rb, QwtPicker::DisplayMode dm, QwtPlotCanvas* canvas):
    6. QwtPlotPicker( xAxis, yAxis, rb , dm , canvas)
    7. {
    8.  
    9. setStateMachine(new QwtPickerPolygonMachine());
    10. setRubberBandPen(QColor(Qt::green));
    11. //d_picker->setRubberBand(QwtPicker::PolygonRubberBand);
    12. setTrackerPen(QColor(Qt::yellow));
    13. //setTrackerFont(f);
    14.  
    15.  
    16.  
    17. }
    18.  
    19.  
    20. QwtText trackerText (const QwtDoublePoint & pos) const;
    21.  
    22. };
    23.  
    24.  
    25. QwtText myPicker::trackerText (const QwtDoublePoint & pos) const
    26. {
    27. QwtText text("(" + QString::number(pos.x()) + "," + QString::number(pos.y()) + ") ");
    28. QColor bgColor(Qt::black);
    29. bgColor.setAlpha(160);
    30. text.setBackgroundBrush(QBrush(bgColor));
    31. return text;
    32. }
    To copy to clipboard, switch view to plain text mode 

    and i created my object like this:

    Qt Code:
    1. d_picker = new myPicker(QwtPlot::xBottom, QwtPlot::yLeft,
    2. QwtPlotPicker::PolygonRubberBand, QwtPicker::AlwaysOn,
    3. myPlot->canvas());
    To copy to clipboard, switch view to plain text mode 

    I have two very basic questions..
    1.how do i use my new function trackerText (const QwtDoublePoint & pos) const
    2.When i use the myPlot private member


    Qt Code:
    1. QwtText text("(" + QString::number(pos.x()) + "," + QString::number(pos.y()) + ") " + QString::number(myPlot.ValueAt(pos.x(), pos.y()))); QColor bgColor(Qt::black);
    To copy to clipboard, switch view to plain text mode 

    i get that myPlot is undeclared. How did FelixB used it with m_spectrogram without getting any error?

  7. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mouse tracking showing float numbers

    Quote Originally Posted by fatecasino View Post
    1.how do i use my new function trackerText (const QwtDoublePoint & pos) const
    you don't have to use it. since QwtPlotPicker::trackerText is declared virtual, it gets called automatically

    Quote Originally Posted by fatecasino View Post
    2.When i use the myPlot private member [...] How did FelixB used it with m_spectrogram without getting any error?
    I passed the plot as parameter to the constructor of myPicker. m_spectrogram is a member of my PlotPicker. But I don't think you need that... it was just a demonstration from my code.

  8. #7
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse tracking showing float numbers

    ok, I removed the QString::number(myPlot.ValueAt(pos.x(), pos.y())

    Qt Code:
    1. class PlotPicker : public QwtPlotPicker
    2. {
    3. public:
    4.  
    5. PlotPicker(QwtPlot::Axis xAxis, QwtPlot::Axis yAxis, QwtPicker::RubberBand rb, QwtPicker::DisplayMode dm, QwtPlotCanvas* canvas):
    6. QwtPlotPicker( xAxis, yAxis, rb , dm , canvas)
    7. {
    8.  
    9. setStateMachine(new QwtPickerPolygonMachine());
    10. setRubberBandPen(QColor(Qt::green));
    11. //d_picker->setRubberBand(QwtPicker::PolygonRubberBand);
    12. //setTrackerPen(QColor(Qt::yellow));
    13. //setTrackerFont(f);
    14. }
    15.  
    16. QwtText trackerText (const QwtDoublePoint & pos) const;
    17.  
    18. };
    19.  
    20. QwtText PlotPicker::trackerText (const QwtDoublePoint & pos) const
    21. {
    22. QwtText text("(" + QString::number(pos.x()) + "...," + QString::number(pos.y()) + ") " );
    23. QColor bgColor(Qt::blue);
    24. bgColor.setAlpha(160);
    25. text.setBackgroundBrush(QBrush(bgColor));
    26. return text;
    27. }
    To copy to clipboard, switch view to plain text mode 

    but the function trackerText is never actually called. I changed several parameters (colour, text, etc) but nothing happens!

  9. #8
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mouse tracking showing float numbers

    show the code where you create and install the picker, please...

  10. #9
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse tracking showing float numbers

    i have created a widget called my2dPlot to plot 2d data sets:
    my2dPlot.h
    Qt Code:
    1. class my2dPlot : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. my2dPlot(QWidget *parent = 0);
    7. ......
    8. private:
    9. QwtPlot *myPlot;
    10. QwtPlotGrid *grid;
    11.  
    12. QwtPlotCurve *mainCurve;
    13. QwtPlotMarker *mainCurveMarker;
    14.  
    15. QwtPlotZoomer *m_zoomer;
    16. QwtPlotPicker *d_picker;//<-----
    17. QwtPlotPanner *d_panner;
    18. ....
    19. }
    To copy to clipboard, switch view to plain text mode 

    then in the my2dPlot.cpp

    Qt Code:
    1. class PlotPicker : public QwtPlotPicker
    2. {
    3. public:
    4. PlotPicker(QwtPlot::Axis xAxis, QwtPlot::Axis yAxis, QwtPicker::RubberBand rb, QwtPicker::DisplayMode dm, QwtPlotCanvas* canvas):
    5. QwtPlotPicker( xAxis, yAxis, rb , dm , canvas)
    6. {
    7.  
    8. setStateMachine(new QwtPickerPolygonMachine());
    9. setRubberBandPen(QColor(Qt::green));
    10. //d_picker->setRubberBand(QwtPicker::PolygonRubberBand);
    11. //setTrackerPen(QColor(Qt::yellow));
    12. //setTrackerFont(f);
    13. }
    14. private:
    15. QwtText trackerText (const QwtDoublePoint & pos) const;
    16.  
    17. };
    18.  
    19. QwtText PlotPicker::trackerText (const QwtDoublePoint & pos) const
    20. {
    21. QwtText text("(" + QString::number(pos.x()) + "...," + QString::number(pos.y()) + ") " );
    22. QColor bgColor(Qt::blue);
    23. bgColor.setAlpha(160);
    24. text.setBackgroundBrush(QBrush(bgColor));
    25. return text;
    26. }
    27. //i have created my own zoomer, copying the "bode" example
    28. class Zoomer: public QwtPlotZoomer
    29. {
    30. public:
    31. Zoomer(int xAxis, int yAxis, QwtPlotCanvas *canvas):
    32. QwtPlotZoomer(xAxis, yAxis, canvas)
    33. {
    34. setTrackerMode(QwtPicker::AlwaysOff);
    35. setRubberBand(QwtPicker::NoRubberBand);
    36.  
    37. // RightButton: zoom out by 1
    38. // Ctrl+RightButton: zoom out to full size
    39.  
    40. setMousePattern(QwtEventPattern::MouseSelect2,
    41. Qt::RightButton, Qt::ControlModifier);
    42. setMousePattern(QwtEventPattern::MouseSelect3,
    43. Qt::RightButton);
    44. }
    45. };
    46.  
    47.  
    48.  
    49. my2dPlot::my2dPlot(QWidget *parent)
    50. : QWidget(parent)
    51. {
    52.  
    53. .....
    54. myPlot = new QwtPlot;
    55. m_zoomer = new Zoomer( QwtPlot::xBottom, QwtPlot::yLeft,myPlot->canvas());
    56. d_picker = new PlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
    57. QwtPlotPicker::PolygonRubberBand, QwtPicker::AlwaysOn,
    58. myPlot->canvas());
    59. .....
    60. }
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mouse tracking showing float numbers

    have you checked if trackerText gets called? I suggest you add a debug output there... (or you set a breakpoint if your ide supports them)

    another idea: do you set a color for the picker? try

    Qt Code:
    1. d_picker->setTrackerPen(QColor(Qt::darkGreen));
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse tracking showing float numbers

    well, as I told you the trackerText never gets called.
    I added a breakpoint but nothing happens.
    I can change the colour of the picker font normally (not if i insert it in the trackerText function)
    any suggestions? check the code above

  13. #12
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mouse tracking showing float numbers

    that's really strange.

    you could try to change the declaration, but I don't think that'll make a difference:
    Qt Code:
    1. PlotPicker *d_picker;//<-----
    To copy to clipboard, switch view to plain text mode 

    this is my initialization:
    Qt Code:
    1. d_picker = new PlotPicker (QwtPlot::xBottom, QwtPlot::yLeft, QwtPicker::PointSelection | QwtPicker::DragSelection, QwtPlotPicker::NoRubberBand, QwtPicker::AlwaysOn, plot->canvas());
    2. d_picker->setTrackerPen(QColor(Qt::darkGreen));
    To copy to clipboard, switch view to plain text mode 

    and my constructor is empty:
    Qt Code:
    1. PlotPicker ::PlotPicker (int xAxis, int yAxis, int selectionFlags, RubberBand rubberBand, DisplayMode trackerMode, QwtPlotCanvas* canvas)
    2. : QwtPlotPicker(xAxis, yAxis, selectionFlags, rubberBand, trackerMode, canvas)
    3. {
    4. }
    To copy to clipboard, switch view to plain text mode 

    and my trackerText is declared "protected" instead of "private", but that definitively does not make a difference (I checked that)

  14. #13
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse tracking showing float numbers

    i have done everything right, but for a very well hidden reason it does not work!


    my2dPlot.h
    Qt Code:
    1. class my2dPlot : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. my2dPlot(QWidget *parent = 0);
    7.  
    8. private:
    9. QwtPlot *myPlot;
    10. QwtPlotGrid *grid;
    11.  
    12. QwtPlotCurve *mainCurve;
    13.  
    14. PlotPicker *d_picker;
    15. ....
    16. }
    To copy to clipboard, switch view to plain text mode 

    my2dPlot.cpp
    Qt Code:
    1. d_picker = new PlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
    2. QwtPlotPicker::PolygonRubberBand, QwtPicker::AlwaysOn,
    3. myPlot->canvas());
    4. d_picker->setStateMachine(new QwtPickerPolygonMachine());
    To copy to clipboard, switch view to plain text mode 

    PlotPicker.h
    Qt Code:
    1. class PlotPicker : public QwtPlotPicker
    2. {
    3. public:
    4.  
    5. PlotPicker (int xAxis, int yAxis, RubberBand rubberBand, DisplayMode trackerMode, QwtPlotCanvas* canvas);
    6.  
    7. private:
    8. QwtText trackerText (const QwtDoublePoint & pos) const;
    9.  
    10. };
    To copy to clipboard, switch view to plain text mode 

    PlotPicker.cpp

    Qt Code:
    1. #include <plotPicker.h>
    2.  
    3. PlotPicker ::PlotPicker (int xAxis, int yAxis, RubberBand rubberBand, DisplayMode trackerMode, QwtPlotCanvas* canvas)
    4. : QwtPlotPicker(xAxis, yAxis, rubberBand, trackerMode, canvas)
    5. {
    6.  
    7. setTrackerPen(QColor(Qt::yellow));//testing if this constructor is called
    8.  
    9.  
    10. }
    11.  
    12.  
    13. QwtText PlotPicker::trackerText (const QwtDoublePoint & pos) const
    14. {
    15. QwtText text("(" + QString::number(pos.x()) + "...," + QString::number(pos.y()) + ") " );
    16. QColor bgColor(Qt::blue);
    17. bgColor.setAlpha(160);
    18. text.setBackgroundBrush(QBrush(bgColor));
    19.  
    20. return text;
    21. }
    To copy to clipboard, switch view to plain text mode 


    The tracker pen is yellow, which means that the constructor of the PlotPicker is called indeed. However, the trackerText function is totally ignored!!
    any ideas?!
    Last edited by fatecasino; 29th January 2011 at 14:58. Reason: reformatted to look better

  15. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,060
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: mouse tracking showing float numbers

    Do you call the QwtPicker::setEnabled( true ) method anywhere?

  16. #15
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse tracking showing float numbers

    yes,I have an "enablePickerMode" button, which enables/disables the picker.
    When I press it I can see that the picker works fine,
    i can select a polygon and I can see the plot values (but the float number like e-12 are represented as 0.0000 and the trackerText function is never called)

    Qt Code:
    1. void my2dPlot::enablePickerMode(bool on)
    2. {
    3.  
    4. d_picker->setEnabled(on);
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

  17. #16
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mouse tracking showing float numbers

    which version of qwt are you using? I have 5.2. Maybe something has changed in 6.0. have a look into the sources of qwtplotpicker.h to and check the declaration of TrackerText()...

  18. #17
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse tracking showing float numbers

    I use the QWT Release 6.0.

    searching in my QWT folder i found this in the qwt_plot_picker.cpp


    Qt Code:
    1. QwtText QwtPlotPicker::trackerText( const QPoint &pos ) const
    2. {
    3. return trackerTextF( invTransform( pos ) );
    4. }
    5.  
    6. /*!
    7.   \brief Translate a position into a position string
    8.  
    9.   In case of HLineRubberBand the label is the value of the
    10.   y position, in case of VLineRubberBand the value of the x position.
    11.   Otherwise the label contains x and y position separated by a ',' .
    12.  
    13.   The format for the double to string conversion is "%.4f".
    14.  
    15.   \param pos Position
    16.   \return Position string
    17. */
    18. QwtText QwtPlotPicker::trackerTextF( const QPointF &pos ) const
    19. {
    20. QString text;
    21.  
    22. switch ( rubberBand() )
    23. {
    24. case HLineRubberBand:
    25. text.sprintf( "%.4f", pos.y() );
    26. break;
    27. case VLineRubberBand:
    28. text.sprintf( "%.4f", pos.x() );
    29. break;
    30. default:
    31. text.sprintf( "%.4f, %.4f", pos.x(), pos.y() );
    32. }
    33. return QwtText( text );
    34. }
    To copy to clipboard, switch view to plain text mode 

    is there an obvious reason I cannot overload these functions? should I just go manually and change them?!


    Added after 15 minutes:


    I FOUND IT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Ι should overload the trackerTextF function and not the trackerText!!!
    I realized it when I saw the source code of the qwt_plot_picker
    Last edited by fatecasino; 1st February 2011 at 20:24.

  19. #18
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,060
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: mouse tracking showing float numbers

    I use the QWT Release 6.0.
    Oh boy. Another "gotcha" when moving from Qwt 5.x to 6.0. I hope these kinds of things are going to be documented...

    Sorry fatecasino, all of our answers were based on Qwt 5.2, where your trackerText() code should have worked perfectly.

Similar Threads

  1. tracking mouse coordinates
    By lightning2911 in forum Newbie
    Replies: 8
    Last Post: 11th December 2011, 23:51
  2. mouse tracking on image
    By vermarajeev in forum Qt Programming
    Replies: 14
    Last Post: 12th May 2010, 13:06
  3. Replies: 14
    Last Post: 16th March 2009, 09:19
  4. mouse tracking in QGraphicsItem
    By christina123y in forum Qt Programming
    Replies: 10
    Last Post: 9th March 2009, 08:23
  5. [QT3+XP] transparency and mouse tracking
    By incapacitant in forum Newbie
    Replies: 9
    Last Post: 17th February 2006, 18:49

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.