尽管Qt::PenStyle给出了一些现成的点划线,但是某些场合下并不能满足我们的需要。此时可以使用QPen::setDashPattern来自定义点划线间隔。
cpp
void QPen::setDashPattern(const QVector<qreal> &pattern)
根据Qt官方文档所说:
The pattern must be specified as an even number of positive entries where the entries 1, 3, 5... are the dashes and 2, 4, 6... are the spaces
pattern必须包含偶数个元素。第1,3,5...元素(从1计数,不从0计数)代表线长度,第2,4,6...代表空白长度。划线时,QPen按照上面的周期重复绘制。
The dash pattern is specified in units of the pens width; e.g. a dash of length 5 in width 10 is 50 pixels long
Pattern中元素的单位不是像素,而是QPen::width()的返回值。比如说,QPen的width是10,而某元素定义为5,那么实际代表的像素数就是5x10=50