正如我的代码片段中的评论所解释的:
对于笔设置为 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]);
}
这是什么原因呢? 如果代码片段中缺少某些信息,请告诉我,我会进行澄清。
最佳答案
您的代码存在多个问题。首先, vector 构造函数的这个重载QVector<qreal>(something)
创建 qreal
的 vector 大小为something
元素,每个元素都将使用默认值进行初始化。
第二,Qt::DashLine
是 enum
解析为 2
的值,所以行 QVector<qreal>(ePenStyl)
创建一个 vector 2 qreal
s,其值为 0。
第三,setDashPattern
并不像你想象的那样工作。这是一个报价from the doc :
Sets the dash pattern for this pen to the given pattern. This implicitly converts the style of the pen to Qt::CustomDashLine.
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.
我想你想做的是
noneSelectedLine->setStyle( ePenStyl );
而不是
noneSelectedLine->setDashPattern(QVector<qreal>(ePenStyl));
关于c++ - 为什么 setwidth 为其他值 1 会使我的线消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33605885/