Results 1 to 7 of 7

Thread: qwtplot and mouse tracking

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2010
    Posts
    15
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qwtplot and mouse tracking

    I have an application with a qwtplot, grid and qwtcurve or histogram_item.h (I think this is irrelevant). I want to show the user the axis values in the plot that correspond to where the cursor is. So I did it like this:

    First I declare an eventfilter from qwtplot to a class that I have, and I activate mousetracking:

    m_plot->setMouseTracking(true);
    m_plot->canvas()->setMouseTracking(true);
    m_plot->installEventFilter(this);

    in the eventFilter I do this (of course after detecting mouse move event):

    statusbar->showMessage( QString("X: ") +
    QString::number((int) (0.5 + m_plot->invTransform(QwtPlot::xBottom, mouseEvent->pos().x()))) +
    QString(" Y: ") +
    QString::number((int) (0.5 + m_plot->invTransform(QwtPlot::yLeft, mouseEvent->pos().y()))));

    so basically, I receive mouse position, and I use invTransform method to convert it from pixel position to the values in the plot.

    The axis are defined like this:

    m_plot->setAxisScale(QwtPlot::yLeft, 0.0, max);
    m_plot->setAxisScale(QwtPlot::xBottom, 0.0, numValues);

    And now my problem. I have many plots (histograms, curves, etc.) and in all of them, I have an error of measure in the X value, this error depends on the position inside the parent (widget or frame) and/or size of the qwtplot. I don't know if the error is already in the pixels or if it is after invTransform. I can correct the difference if I don't allow to resize the plot, but I want to know where this error comes from.

    If you need more code or information, please ask for it, and thanks for your help.

  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: qwtplot and mouse tracking

    You need to filter the mouse events from the plot canvas not from the plot widget itsself.
    Nevertheless have a look at QwtPlotPicker before you continue.

    Uwe

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

    fearu (10th October 2010)

  4. #3
    Join Date
    Aug 2010
    Posts
    15
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qwtplot and mouse tracking

    Thanks for your answer. I've been busy lately so I haven't tried it. I'll post here if it worked when I have time to work on it.

  5. #4
    Join Date
    Aug 2010
    Posts
    15
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qwtplot and mouse tracking

    It worked filtering from canvas(). I won't use qwtplotpicker as the current code works, but I will use it the next time. Thanks again.

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

    Default Re: qwtplot and mouse tracking

    Is it possible to use a QwtPlotPicker to show something in the statusbar? I have a Spectrogram and want to show the z-value to the current coordinates (and other things, but that is not so important at the moment). I tried to use "moved"-signal, but that works only if the mouse is clicked (as in "bode"-example).

    How can I change that?

  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: qwtplot and mouse tracking

    got it I had to write my own subclass of QwtPlotPicker and overriding the "trackerText" method:

    Qt Code:
    1. class MyPicker : public QwtPlotPicker
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyPicker(int xAxis, int yAxis, int selectionFlags, RubberBand rubberBand, DisplayMode trackerMode, QwtPlotCanvas* canvas);
    7.  
    8. signals:
    9. void mouseMoved(const QPoint& pos) const;
    10.  
    11. protected:
    12. virtual QwtText trackerText (const QwtDoublePoint & pos) const;
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MyPicker::MyPicker(int xAxis, int yAxis, int selectionFlags, RubberBand rubberBand, DisplayMode trackerMode, QwtPlotCanvas* canvas)
    2. : QwtPlotPicker(xAxis, yAxis, selectionFlags, rubberBand, trackerMode, canvas)
    3. {}
    4.  
    5. QwtText MyPicker::trackerText (const QwtDoublePoint & pos) const
    6. {
    7. const QPoint point = pos.toPoint();
    8. emit mouseMoved(point);
    9. return QwtText(QString::number(point.x()) + ", " + QString::number(point.y()));
    10. }
    To copy to clipboard, switch view to plain text mode 

    now I can react to the "mouseMoved"-signal

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

    Default Re: qwtplot and mouse tracking

    I'm not sure if this is related to QwtPlotPicker or general to Qt...

    as written above, I want to react to the mouseMoved-signal. Currently, I write text into the statusBar. But I want to do this only as long as the mouse is on my plot. When the user moves the mouse outside the canvas, I don't want to do anything. Is there a possibility to check if the cursor is on the canvas? Or the get a signal when the cursor has left the canvas?

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. Mouse tracking outside the application interface
    By sophister in forum Qt Programming
    Replies: 7
    Last Post: 2nd May 2009, 06:44
  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

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.