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

{

public:

void updateLegend(QwtLegend *legend) const;

FractionCurve(const QString);

};



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

{

;

}



void FractionCurve::updateLegend(QwtLegend *legend) const

{

QwtPlotCurve::updateLegend(legend);

QwtLegendItem *lgdItem =

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

if ( lgdItem )

lgdItem->setIdentfierWidth(24);

}

And an example of a plot with the new legend:

1 comment:

  1. Thanks! This is exactly what I was trying to do!
    I left off the 'const' at first and the changes didn't take (it was exactly the same as before).
    So don't forget the const at the end!

    ReplyDelete