Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom dash style in QPen looking dirty

Tags:

c++

qt

qt5

qpen

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?

like image 294
Maximko Avatar asked Sep 19 '25 19:09

Maximko


1 Answers

Solution found. It's all about antialiasing.

Using:

painter->setRenderHint(QPainter::Antialiasing)

solves the problem.

like image 73
Maximko Avatar answered Sep 22 '25 10:09

Maximko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!