I need to draw dashed lines in Qt with QPen
, and I want to set custom dash pattern with QPen::setDashPattern():
QPen pen;
pen.setColor(Qt::black);
QVector<qreal> dashes;
dashes << 1.0 << 8.0;
pen.setDashPattern(dashes);
pen.setCapStyle(Qt::RoundCap);
pen.setWidthF(1.02);
painter->setPen(pen);
...
I get the pattern I need, but the line looks "dirty": some dashes seem to have a bit more weight to them than others:
Only if I do not use setWidthF(), or use it with setWidthF(1.0), the lines look clean and even. But if I increase the width even a little bit, the lines become spoiled. Is it possible to get clean and even lines with custom dash and arbitrary width?
Solution found. It's all about antialiasing.
Using:
solves the problem.