Custom dash style in QPen looking dirty

2k views Asked by At

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:

enter image description here

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?

1

There are 1 answers

0
Maximko On BEST ANSWER

Solution found. It's all about antialiasing.

Using:

painter->setRenderHint(QPainter::Antialiasing)

solves the problem.