5 Attachment(s)
Patterned Qwt Legend Line and Plot Line look different if thickness is greater than 1
We do have a fix to the problem we were having with Qwt 6.1.3 (built with Qt 5.5.1) on Windows. This with the standard QwtLegend implementation. The fix is: commenting out the call to QPen::setCapStyle (Qt::FlatCap) in method QwtPlotCurve::legendIcon().
Uwe, and anyone else who knows about this. Is this fix (omitting call to setCapStyle) the right idea? Or am I missing something?
The two following screenshots show the discrepancy. Both have a line width of 2. Dotted, and Dashed:
Attachment 12411
Attachment 12412
http://cadswes2.colorado.edu/~philw/...-Prob-Dot2.png
http://cadswes2.colorado.edu/~philw/...Prob-Dash2.png
Here's a closer look at the problem. The "fixed" version matches what is shown on the plot:
Attachment 12413
http://cadswes2.colorado.edu/~philw/...LegendProb.png
The following show a fix -- effectively commenting out this line in QwtPlotCurve::legendIcon():
- pn.setCapStyle( Qt::FlatCap );
Attachment 12414
Attachment 12415
http://cadswes2.colorado.edu/~philw/...3-Fix-Dot2.png
http://cadswes2.colorado.edu/~philw/...-Fix-Dash2.png
Instead of actually modifying QwtPlotCurve::legendIcon(), we lifted that implementation out of that class, and implemented a modified version in our QwtPlotCurve subclass.
Code:
// virtual from QwtPlotCurve
QwtGraphic SlotCurve::legendIcon (int index, // (ignore, only one)
const QSizeF& iconSize) const
{
// call base class method
//-- QwtGraphic graphic = QwtPlotCurve::legendIcon (index, iconSize);
// Note [Phil, 3-2017, RW 7.1, Qwt 6.1.3, Qt 5.5.1, Gnats 5864]:
// The following code is adapted from QwtPlotCurve::legendIcon().
// See the fix for Gnats 5864, below ("Legend and plot line look
// different if thickness is greater than one").
Q_UNUSED( index );
if ( iconSize.isEmpty() )
return QwtGraphic();
QwtGraphic graphic;
graphic.setDefaultSize( iconSize );
graphic.setRenderHint( QwtGraphic::RenderPensUnscaled, true );
painter.
setRenderHint( QPainter::Antialiasing,
bool doShowLine
= testLegendAttribute
(QwtPlotCurve::LegendShowLine);
bool doShowSymb
= testLegendAttribute
(QwtPlotCurve::LegendShowSymbol);
bool doShowBrush
= testLegendAttribute
(QwtPlotCurve::LegendShowBrush);
bool noAttribs = !(doShowLine || doShowSymb || doShowBrush);
const QPen curvePen
= pen
();
if ( noAttribs || doShowBrush)
{
if ( curveBrush.style() == Qt::NoBrush && noAttribs )
{
{
curveBrush
= QBrush( curvePen.
color() );
}
else if ( curveSymb
&& (curveSymb
->style
() != QwtSymbol::NoSymbol) ) {
curveBrush
= QBrush( curveSymb
->pen
().
color() );
}
}
if ( curveBrush.style() != Qt::NoBrush )
{
QRectF r
( 0,
0, iconSize.
width(), iconSize.
height() );
painter.fillRect( r, curveBrush );
}
}
if ( doShowLine )
{
if ( curvePen != Qt::NoPen )
{
// CADSWES FIX, Gnats 5864 ("Legend and plot line look different if
// thickness is greater than one"). DON'T set the pen Cap Style.
//
//-- QPen pn = curvePen;
//-- pn.setCapStyle( Qt::FlatCap );
//-- painter.setPen( pn );
painter.setPen( curvePen );
const double y = 0.5 * iconSize.height();
QwtPainter::drawLine( &painter,
0.0, y, iconSize.
width(), y
);
}
}
if ( doShowSymb )
{
if ( curveSymb )
{
QRectF r
( 0,
0, iconSize.
width(), iconSize.
height() );
curveSymb->drawSymbol( &painter, r );
}
}
return graphic;
}
Re: Patterned Qwt Legend Line and Plot Line look different if thickness is greater th
If you prefer to keep the cap style from the curve sure. To be honest I can't remember the reason behind explicitly setting FlatCap - maybe because of trying to get rid of the extra pixels at the ends, that make drawing the line inaccurate.
Uwe
Re: Patterned Qwt Legend Line and Plot Line look different if thickness is greater th
Oh wow. Uwe, that's interesting.
We didn't intend to set a Qt::PenCapStyle on our QwtPlotCurves' pens. We do want our curves to be as precise as possible, and would want to stick with Qt::FlatCap. We aren't explicitly doing anything with cap style, but I am understanding that a defaulted constructed QPen -- or one constructed from a color or pen style (e.g. Dotted) -- has a cap style of Qt::SquareCap. (We must be assigning a QPen like that to our QwtPlotCurves, but I haven't found that in our code yet).
I DO see that when I force our QwtPlotCurves to have the Qt::FlatCap cap style -- see below -- the QwtLegend does show pen styles (e.g. Dotted) consistently with the plotted curve (with my modified code omitting the setting of the legend line's cap style -- see my prior post). So that explicit setting of Qt::FlatCap in QwtPlotCurve::legendIcon() I found seems to be unnecessary (at least now, in Qwt 6.1.3) [using Qt 5.5.1].
Code:
curvePen.setCapStyle (Qt::FlatCap);
setPen (curvePen);