Results 1 to 4 of 4

Thread: qwtWidgetOverlay going into left axis scale

  1. #1
    Join Date
    Dec 2011
    Posts
    55
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default qwtWidgetOverlay going into left axis scale

    I'm trying to use a QwtWidgetOverlay for a rectangle that I'm using to highlight a plot region. The issue I'm having is that the overlay is not disappearing at the left edge of the plot, but being allowed to go all the way to the left edge of the y-axis scale / label.

    On the right edge of the plot, the overlay disappears properly at the plot's edge.



    My code puts a QwtPlotItem (the rectangle) inside of a QwtWidgetOverlay, and I'm using plot->canvas()->contentsRect() to get the canvas boundaries.

    Any pointers would be greatly appreciated.

    Qt Code:
    1. class RectangleOverlay: public QwtPlotItem
    2. {
    3.  
    4. public:
    5. RectangleOverlay(QwtPlot *plot, const QwtText &title = QwtText());
    6. virtual ~RectangleOverlay();
    7.  
    8. virtual void draw (QPainter *painter,
    9. const QwtScaleMap &xMap,
    10. const QwtScaleMap &yMap,
    11. const QRectF &canvasRect) const override
    12. {
    13. int x1 = qRound(xMap.transform(m_interval.minValue()));
    14. int x2 = qRound(xMap.transform(m_interval.maxValue()));
    15.  
    16. // Fill color
    17. QColor blue("RoyalBlue");
    18. blue.setAlpha(125);
    19. QBrush brush(blue);
    20.  
    21. // Rectangle
    22. QPen pen("RoyalBlue");
    23. pen.setColor(pen.color().lighter());
    24. pen.setWidth(m_edgeWidth);
    25. painter->setPen(pen);
    26.  
    27. // Add penWidth's pixels to the top and bottom of the rectangle
    28. // so only the side lines are visible
    29. QRect overlayRect(x1, canvasRect.top()-m_edgeWidth, x2 - x1, canvasRect.height()+m_edgeWidth);
    30. painter->drawRect(overlayRect);
    31. painter->fillRect(overlayRect, brush);
    32. }
    33.  
    34. virtual QRectF boundingRect() const override
    35. {
    36. const QwtScaleMap xMap = m_plot->canvasMap(xAxis());
    37.  
    38. int x1 = qRound(xMap.transform(m_interval.minValue()));
    39. int x2 = qRound(xMap.transform(m_interval.maxValue()));
    40.  
    41. QRectF overlayRect(x1 - m_edgeWidth,
    42. m_plot->canvas()->contentsRect().top(),
    43. x2 - x1 + (2*m_edgeWidth),
    44. m_plot->canvas()->contentsRect().height());
    45.  
    46. return overlayRect;
    47. }
    48.  
    49. void setInterval(QwtInterval &interval) {m_interval = interval;}
    50. QwtInterval getInterval() const {return m_interval;}
    51.  
    52. protected:
    53.  
    54. private:
    55. QwtPlot *m_plot;
    56. QwtInterval m_interval;
    57. int m_edgeWidth;
    58. };
    59.  
    60.  
    61. class Overlay: public QwtWidgetOverlay
    62. {
    63. public:
    64. Overlay(QWidget *parent)
    65. : QwtWidgetOverlay(parent)
    66. , m_plot(dynamic_cast<const QwtPlot*>(parent))
    67. {
    68.  
    69. }
    70. virtual ~Overlay() {};
    71.  
    72. void attachPlotItem(QwtPlotItem *plotItem) {m_plotItem = plotItem;}
    73.  
    74. protected:
    75. virtual void drawOverlay( QPainter *painter ) const
    76. {
    77. if (!m_plot)
    78. return;
    79.  
    80. const QwtScaleMap xMap = m_plot->canvasMap(m_plotItem->xAxis());
    81. const QwtScaleMap yMap = m_plot->canvasMap(m_plotItem->yAxis());
    82.  
    83. QRectF rect(m_plot->canvas()->contentsRect());
    84.  
    85. if (m_plotItem)
    86. m_plotItem->draw(painter, xMap, yMap, m_plot->canvas()->contentsRect());
    87. }
    88.  
    89. virtual QRegion maskHint() const
    90. {
    91. if (!m_plot)
    92. return QRegion();
    93.  
    94. QRectF boundingRect = m_plotItem->boundingRect();
    95.  
    96. return QRegion(boundingRect.x(), boundingRect.y(), boundingRect.width(), boundingRect.height());
    97. }
    98.  
    99. private:
    100. const QwtPlot *m_plot;
    101. QwtPlotItem *m_plotItem;
    102. };
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

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

    Default Re: qwtWidgetOverlay going into left axis scale

    Use the plot canvas as parent.

    HTH,
    Uwe

  3. #3
    Join Date
    Dec 2011
    Posts
    55
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qwtWidgetOverlay going into left axis scale

    Thanks, Uwe, that fixed the bounding issue!

    However, a quick use-model question on QwtWidgetOverlay.

    I notice that in your Editor example you're attaching the QwtPlotItem directly to the canvas and then creating and destroying a temporary QwtWidgetOverlay when the item is moved.

    Is this the intended use-model?

    In my implementation, I never attach() the QwtPlotItem to the plot. I just manually call updateOverlay() whenever the overlay is dragged or moved or replot() is called.

    This seems to work fine but I'm wondering if I'm doing it wrong.
    Last edited by alketi; 15th June 2021 at 22:18.

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

    Default Re: qwtWidgetOverlay going into left axis scale

    Well, an overlay is not part of what is seen by QwtPlotRenderer.

    Uwe

Similar Threads

  1. Replies: 5
    Last Post: 5th June 2018, 08:06
  2. Replies: 0
    Last Post: 23rd January 2017, 11:55
  3. Replies: 1
    Last Post: 9th September 2013, 07:50
  4. Y Axis tick labels cut off on left
    By rbergeron in forum Qwt
    Replies: 2
    Last Post: 4th November 2011, 14:29
  5. Replies: 4
    Last Post: 16th January 2011, 10:32

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.