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.
Code:
void XRealTimeLegendItem
::drawText(QPainter *painter,
const QRect &rect
) {
int maxItemWidth = map.screenToLayoutX( fontMetrics.width( maxItemName ) );
painter->drawText( rect, text().text() );
valueRect.setX( rect.right() - rect.width() + maxItemWidth + 2 * spacing() );
painter->drawText( valueRect, currentValue );
}
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 ?
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
Re: How to draw aligned elements next to QwtLegendItem
Quote:
Originally Posted by
Uwe
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.
Code:
void XRealTimeCurve::updateLegendItem()
{
QString legendItemValue
= QString().
setNum( y
( dataSize
() - 1 ),
'f',
2 );
int maxWidth = fontMetrics.width( maxPenName ) + 10;
QString table
= "<table> <tr> <td width="+ QString().
setNum( maxWidth
) + ">" + legendItemName
+ "</td> <td width=50>" + legendItemValue + "</td> </tr> </table>";
}
Now it needn't subclass QwtLegendItem::drawText with complicated code. It works fine, thanks.
Re: How to draw aligned elements next to QwtLegendItem
For updating the curve title updateLegendItem() is the wrong place !
Uwe
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.
Code:
{
public:
void setLegendItemName( const QString& name );
private:
}
void XTrendViewer::updateCurveTitle( XTrendCurve* curve )
{
int dataSize = curve->dataSize();
if ( dataSize == 0 )
{
curve->setTitle( curve->legendItemName() );
return;
}
QPointF currentPoint
= curve
->sample
( dataSize
- 1 );
QString legendItemValue
= QString().
setNum( currentPoint.
y(),
'f',
2 );
int maxWidth = fontMetrics.width( maxPenName ) + 10;
QString table
= "<table> <tr> <td width="+ QString().
setNum( maxWidth
) + ">" + curve
->legendItemName
() + "</td> <td width=50>" + legendItemValue + "</td> </tr> </table>";
}
I set title and legend item's text from external place in class XTrendViewer : public QwtPlot