Results 1 to 5 of 5

Thread: How to draw aligned elements next to QwtLegendItem

  1. #1
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to draw aligned elements next to QwtLegendItem

    Hi.

    I have the next problem. I need draw number next to text of legend's item.

    This number is changeable and all numbers must be aligned in one column for each legend's item.

    For example:

    first legend item 23.00
    second legend item 900.00
    third legend item 3.00


    I tried to implement the next solution. I subclassed QwtLegenItem and overrided function drawText. Then I was finding the legend's item with maximal width, converted its width in pixels with help QwtMetricsMap and used function drawText several times.
    Qt Code:
    1. void XRealTimeLegendItem::drawText(QPainter *painter, const QRect &rect)
    2. {
    3. QString currentValue = QString().setNum( currentItemValue->value() );
    4.  
    5. QFontMetrics fontMetrics = painter->fontMetrics();
    6. const QwtMetricsMap &map = QwtPainter::metricsMap();
    7.  
    8. int maxItemWidth = map.screenToLayoutX( fontMetrics.width( maxItemName ) );
    9.  
    10. painter->drawText( rect, text().text() );
    11.  
    12. QRect valueRect = rect;
    13. valueRect.setX( rect.right() - rect.width() + maxItemWidth + 2 * spacing() );
    14. painter->drawText( valueRect, currentValue );
    15. }
    To copy to clipboard, switch view to plain text mode 

    But print of QwtPlot doesn't work for this solution. The result of print is

    first legend item
    second legend item
    third legend item

    without numbers!

    How can I draw numbers aligned in one column with correct print ?
    Last edited by maxomato; 23rd February 2011 at 13:20. Reason: updated contents

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

    Default Re: How to draw aligned elements next to QwtLegendItem

    Did you reimplement QwtPlotCurve::legendItem() returning a XRealTimeLegendItem ?

    Of course you also have to implement XRealTimeLegendItem::sizeHint() to add the extra pixels you need to draw your numbers.

    But probably it's easier to use rich text for the alignment.

    Uwe

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

    maxomato (24th February 2011)

  4. #3
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to draw aligned elements next to QwtLegendItem

    Quote Originally Posted by Uwe View Post
    Did you reimplement QwtPlotCurve::legendItem() returning a XRealTimeLegendItem ?

    Of course you also have to implement XRealTimeLegendItem::sizeHint() to add the extra pixels you need to draw your numbers.

    But probably it's easier to use rich text for the alignment.

    Uwe
    Yes, I reimplemented QwtPlotCurve::legendItem.

    But your method with rich text is really easier.

    I subclassed QwtPlotCurve and defined function updateLegendItem and used <table> tag in rich text.
    Qt Code:
    1. void XRealTimeCurve::updateLegendItem()
    2. {
    3. QString legendItemValue = QString().setNum( y( dataSize() - 1 ), 'f', 2 );
    4.  
    5. QFontMetrics fontMetrics( QwtText().font() );
    6.  
    7. int maxWidth = fontMetrics.width( maxPenName ) + 10;
    8.  
    9. QString table = "<table> <tr> <td width="+ QString().setNum( maxWidth ) + ">" + legendItemName + "</td> <td width=50>" +
    10. legendItemValue + "</td> </tr> </table>";
    11.  
    12. setTitle( QwtText( table, QwtText::RichText ) );
    13. }
    To copy to clipboard, switch view to plain text mode 
    Now it needn't subclass QwtLegendItem::drawText with complicated code. It works fine, thanks.

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

    Default Re: How to draw aligned elements next to QwtLegendItem

    For updating the curve title updateLegendItem() is the wrong place !

    Uwe

  6. #5
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to draw aligned elements next to QwtLegendItem

    Yes, I moved the code from XRealTimeCurve and now I am saving legendItemName in XRealTimeCurve only.

    Qt Code:
    1. class XRealTimeCurve : public QwtPlotCurve
    2. {
    3. public:
    4. void setLegendItemName( const QString& name );
    5.  
    6. QString legendItemName() const;
    7. private:
    8. QString legendItem;
    9. }
    10.  
    11. void XTrendViewer::updateCurveTitle( XTrendCurve* curve )
    12. {
    13. int dataSize = curve->dataSize();
    14. if ( dataSize == 0 )
    15. {
    16. curve->setTitle( curve->legendItemName() );
    17. return;
    18. }
    19.  
    20. QPointF currentPoint = curve->sample( dataSize - 1 );
    21. QString legendItemValue = QString().setNum( currentPoint.y(), 'f', 2 );
    22.  
    23. QFontMetrics fontMetrics( QwtText().font() );
    24.  
    25. int maxWidth = fontMetrics.width( maxPenName ) + 10;
    26.  
    27. QString table = "<table> <tr> <td width="+ QString().setNum( maxWidth ) + ">" + curve->legendItemName() + "</td> <td width=50>" +
    28. legendItemValue + "</td> </tr> </table>";
    29.  
    30. curve->setTitle( QwtText( table, QwtText::RichText ) );
    31. }
    To copy to clipboard, switch view to plain text mode 

    I set title and legend item's text from external place in class XTrendViewer : public QwtPlot

Similar Threads

  1. Replies: 0
    Last Post: 18th January 2011, 02:19
  2. Painting a right-aligned text
    By curreli in forum Newbie
    Replies: 2
    Last Post: 29th July 2010, 16:49
  3. QwtPlotItem inverting qwtPlot scale
    By jmsbc in forum Qwt
    Replies: 1
    Last Post: 25th August 2009, 14:09
  4. Replies: 3
    Last Post: 16th May 2009, 10:16

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.