Results 1 to 3 of 3

Thread: Problem rendering custom QwtPlotLegendItem

  1. #1
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem rendering custom QwtPlotLegendItem

    Hi everyone,

    I want to have a QwtPlotLegendItem fully positionable, not only with Qt::Alignment and setMargins.
    So, in the legends example, I modified the class Legend like this:
    Qt Code:
    1. class LegendItem: public QwtPlotLegendItem
    2. {
    3. QPoint m_pos;
    4. public:
    5. LegendItem()
    6. {
    7. setRenderHint( QwtPlotItem::RenderAntialiased );
    8. QColor color( Qt::white );
    9. setTextPen( color );
    10. #if 1
    11. setBorderPen( color );
    12.  
    13. QColor c( Qt::gray );
    14. c.setAlpha( 200 );
    15.  
    16. setBackgroundBrush( c );
    17.  
    18. #endif
    19. setPosition(QPoint(0,25));
    20. }
    21. virtual QRect geometry(const QRectF &canvasRect) const {
    22. QRect rect = QwtPlotLegendItem::geometry( canvasRect );
    23. rect.moveTopLeft( m_pos );
    24. return rect;
    25. }
    26. void setPosition(const QPoint &pos){
    27. m_pos = pos;
    28. itemChanged();
    29. }
    30. };
    To copy to clipboard, switch view to plain text mode 

    It works fine on the plot, the legend top left corner is at position (0,25) of the canvas, but when I export in a pdf file using QwtPlotRenderer, then the position of the legend is not correct (see the attached file), it is shifted by I don't know how much in the left and top direction. What am I doing wrong here?

    Thank you for your help
    Attached Files Attached Files

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

    Default Re: Problem rendering custom QwtPlotLegendItem

    m_pos is used as an absolute offset from (0, 0), but it needs to be relative to canvasRect.topLeft().

    When drawing to screen canvasRect is the contentsRect() of the canvas ( rect() without margins ), but when using QwtPlotRenderer it is a rectangle somewhere inside a layout for the complete plot.

    Uwe

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

    LoranM (13th September 2013)

  4. #3
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem rendering custom QwtPlotLegendItem

    Thank you Uwe,

    Here is the modified code in LegendItem class:
    Qt Code:
    1. virtual QRect geometry( const QRectF &canvasRect ) const
    2. {
    3. QRect rect = QwtPlotLegendItem::geometry( canvasRect );
    4. rect.moveTopLeft( m_pos + canvasRect.topLeft().toPoint());
    5. return rect;
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by LoranM; 13th September 2013 at 13:46. Reason: updated contents

Similar Threads

  1. A suggestion about QwtPlotLegendItem
    By Nicho in forum Qwt
    Replies: 4
    Last Post: 7th April 2015, 12:06
  2. QwtPlotLegendItem and QwtLegendItem
    By jesse_mark in forum Qwt
    Replies: 3
    Last Post: 14th November 2012, 07:10
  3. Replies: 8
    Last Post: 7th November 2012, 19:20
  4. Replies: 2
    Last Post: 30th July 2009, 04:58
  5. Rendering problem
    By boss_bhat in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2006, 08:48

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.