Sunday, September 26, 2010

Change legend line width in Qwt

In the past I made a small program that basically just plots a number of columns from a text file. This program is written using the Qwt plotting library. The program can simultaneously plot the columns from multiple text files. The different columns in a file are given different colours and for columns from different files a different line style is used (normal, dashed, dotted or dash-dot). A legend showing which line is which is also displayed. Here I had the problem that with the default legend properties the width of the line shown in the legend is so small that you can not distinguish between a normal and a dashed or dashed dotted line. After some searching on the web I found a good description of how to change the width of the line in the legend. If you follow the link you find that they call the line section displayed the legend icon (that is why it took me some time to find that website). In the end I modified their code a little bit. My program was using plain QwtPlotCurve variables. So I made a derived class FractionCurve which implements the updateLegend function. The total code of this very simple custom Curve class is given below. Because my program uses the QwtPlotCurve constructor that takes a QString as an argument I also had to implement that constructor for the derived FractionCurve class.

#include <qwt_legend.h>

#include <qwt_legend_item.h>

class FractionCurve : public QwtPlotCurve



void updateLegend(QwtLegend *legend) const;

FractionCurve(const QString);


FractionCurve::FractionCurve(const QString s) : QwtPlotCurve(s)




void FractionCurve::updateLegend(QwtLegend *legend) const



QwtLegendItem *lgdItem =

dynamic_cast<QwtLegendItem*>(legend->find( this ));

if ( lgdItem )



And an example of a plot with the new legend: