由于我的代码段中的评论已经解释为:
对于笔设置为noneSelectedLine
且笔宽为!= 1的行,代码无法正常工作。
scene = new QGraphicsScene();
Qt::PenStyle ePenStyl = Qt::DashLine;
selectedLine = new QPen(Qt::blue);
noneSelectedLine = new QPen(Qt::red);
selectedLine->setWidth(2);
noneSelectedLine->setWidth(1);
noneSelectedLine->setDashPattern(QVector<qreal>(ePenStyl));
/*If this line is a comment all is running as expected, but as soon as I
set in the following line, all lines where the pen is set to
noneSelectedLine they are not drawn (or at least not visible). What could
be the reason for that?*/
//noneSelectedLine->setWidth(3);
for (int indexI = 0; indexI < 5; indexI++)
{
scene->addItem(&LineSet[indexI]);
}
这可能是什么原因? 如果代码段中缺少某些信息,请告诉我,我会澄清。
答案 0 :(得分:2)
您的代码存在多个问题。首先,向量构造函数QVector<qreal>(something)
的这个重载创建了一个qreal
向量,其大小为something
个元素,每个元素都将使用默认值进行初始化。
其次,Qt::DashLine
是一个enum
值,可以解析为2
,因此行QVector<qreal>(ePenStyl)
会创建一个2 qreal
s的向量,其值为这将是0。
第三,setDashPattern
不会像您认为的那样有效。这是引用from the doc:
将此笔的虚线模式设置为给定模式。这会隐式地将笔的样式转换为Qt :: CustomDashLine。
必须将模式指定为偶数个正条目,其中条目1,3,5 ...是短划线,而2,4,6 ......是空格。
我认为你想要做的是
noneSelectedLine->setStyle( ePenStyl );
而不是
noneSelectedLine->setDashPattern(QVector<qreal>(ePenStyl));