Results 1 to 4 of 4

Thread: Adding marker to scale widget

  1. #1
    Join Date
    Jan 2014
    Posts
    36
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Adding marker to scale widget

    Is there a convenient way to add a marker to the scale widget? I've got a vertical marker enabled on the plot itself, but I'd like for that marker to be extended onto the x-axis scale widget as well.

    I didn't see any obvious function, but it looks like maybe I could use something like QwtScaleDraw::drawTick() to help?

  2. #2
    Join Date
    Dec 2013
    Location
    Toronto, Canada
    Posts
    62
    Thanked 16 Times in 15 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adding marker to scale widget

    but it looks like maybe I could use something like QwtScaleDraw::drawTick() to help?
    That's exactly what I did a little while back..

    Qt Code:
    1. class ScaleDraw: public QwtScaleDraw
    2. {
    3. public:
    4. ScaleDraw(QwtPlotMarker* marker): m_marker(marker)
    5. {
    6.  
    7. }
    8. protected:
    9. void drawTick (QPainter *p, double val, double len) const
    10. {
    11. p->save();
    12. p->setPen(m_marker->linePen());
    13. double x= m_marker->xValue();
    14. x = m_marker->plot()->transform(QwtPlot::xBottom, x);
    15. p->drawLine(x-1,0, x-1, 50); //x-1 instead of x is a little tweak
    16. p->restore();
    17. QwtScaleDraw::drawTick (p, val, len);
    18. }
    19. private:
    20. QwtPlotMarker* m_marker;
    21.  
    22. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. QwtPlot * plot = new QwtPlot();
    6. marker->setLinePen("red");
    7. plot->setAxisScaleDraw(QwtPlot::xBottom, new ScaleDraw(marker));
    8. marker->setLineStyle( QwtPlotMarker::VLine);
    9. marker->setXValue(420);
    10. marker->attach(plot);
    11. plot->replot();
    12. plot->show();
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    I did not test the code snippets... Let me know how it went

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

    embeddedmz (25th January 2020)

  4. #3
    Join Date
    Jan 2014
    Posts
    36
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Adding marker to scale widget

    Thanks for the reply, although now that I'm playing with your sample, I'm realizing I asked my question a bit incorrectly. First, I'm using a QwtPlotPicker, not a QwtPlotMarker, but I'm using the picker as a vertical moving cursor position, not actually using it to select anything. So I want the additional tick mark that I'm drawing on the scale widget to update in real-time whenever the mouse moves, just like the plot picker does on the actual plot.

    The bigger picture: I've got multiple vertically aligned plots. All these plots have a common timescale, but completely different vertical scales. As the user moves the mouse over any individual plot, I emit a signal of the x-position, and then draw a plot marker on all the other plots, so that it looks like there's one big vertical cursor that is moving through all the plots. Right now, I've got the x-axis enabled on all the plots, but since it's the same timescale on each plot, I'm just wasting a lot of vertical pixels displaying the same information multiple times. So I'd like to move to a single scale widget, but have that cursor show up on it as well, just to help visually tie everything together.

  5. #4
    Join Date
    Jan 2014
    Posts
    36
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Adding marker to scale widget

    Ok, it appears that I've got it working the way I want, I had to make a few tweaks to your code, here's what I'm using now:
    Qt Code:
    1. class ScaleDraw: public QwtDateScaleDraw
    2. {
    3. public:
    4. ScaleDraw(QwtPlotMarker* marker): m_marker(marker)
    5. {
    6. }
    7. void invalidate()
    8. {
    9. invalidateCache();
    10. }
    11. protected:
    12. void drawTick (QPainter *p, double val, double len) const
    13. {
    14. if (mMarker && mMarker->isVisible()) // visibility of this follows that of plot marker
    15. {
    16. p->save();
    17. p->setPen(mMarker->linePen());
    18. double x = mMarker->xValue();
    19. x = scaleMap().transform(x);
    20. p->drawLine(x, 0, x, 10);
    21. p->restore();
    22. }
    23. QwtScaleDraw::drawTick (p, val, len);
    24. }
    25. private:
    26. QwtPlotMarker* m_marker;
    27.  
    28. };
    To copy to clipboard, switch view to plain text mode 

    The big change I had to make was where you transform the marker's x value into the pixel's x-coordinate, previously you had:
    Qt Code:
    1. x = m_marker->plot()->transform(QwtPlot::xBottom, x);
    2. p->drawLine(x-1, 0, x-1, 10);
    To copy to clipboard, switch view to plain text mode 
    but that didn't seem to work correctly, the line on the plot wasn't lining up with the line on the scale widget. I played around with it a little, and noticed that the amount of pixels I was off by varied as the number of characters in the scale's leftmost label changed, which makes sense because the lowest valued tick is centered on that label text. So, switching to:
    Qt Code:
    1. x = scaleMap().transform(x);
    2. p->drawLine(x, 0, x, 10);
    To copy to clipboard, switch view to plain text mode 
    seemed to work better, and doesn't require the x-1 you were doing before.

    My class that inherits from a QwtPicker emits a custom signal time(double x) whenever the cursor moves over the plot, where x is x value of the picker (in scaled values). I connect that to:
    Qt Code:
    1. void MainWindow::slotIndex(const double &pos)
    2. {
    3. if (pos == mPrevPos)
    4. {
    5. // no need to do anything if we were triggered because of a y axis move
    6. return;
    7. }
    8. mPrevPos = pos;
    9. if (xLimitMin <= pos && pos <= xLimitMax)
    10. {
    11. marker->setXValue(pos);
    12. marker->show();
    13. markedScaleDraw* sd = dynamic_cast<markedScaleDraw*>(centralPlot->axisScaleDraw(QwtPlot::xBottom));
    14. if (sd)
    15. {
    16. sd->invalidate(); // clear the scale draw's cache
    17. }
    18. } else {
    19. marker->hide();
    20. }
    21. centralPlot->replot();
    22. centralPlot->axisWidget(QwtPlot::xBottom)->update(); // cause scale draw to repaint
    23. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Adding Scale in Qt Painting
    By avanindra in forum Qt Programming
    Replies: 3
    Last Post: 19th July 2012, 10:30
  2. Replies: 1
    Last Post: 3rd November 2009, 23:26
  3. Marker and Scale
    By WXNSNW in forum Qt Programming
    Replies: 0
    Last Post: 11th December 2008, 10:44
  4. Scale a widget
    By yagabey in forum Qt Programming
    Replies: 2
    Last Post: 7th December 2008, 14:08
  5. My own scale widget in Qwt
    By igrms in forum Qwt
    Replies: 7
    Last Post: 15th June 2006, 22:18

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.