为什么将宽度设置为其他1会使我的行消失

Why setwidth to other then 1 makes my line disappear?

本文关键字:我的 消失 其他 设置 为什么      更新时间:2023-10-16

正如我的代码片段allready中的注释所解释的:

对于笔宽度为的笔设置为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]);
}

这里会有什么原因?如果片段中缺少一些信息,请告诉我,我会澄清的。

您的代码存在多个问题。首先,向量构造函数QVector<qreal>(something)的这种重载创建了一个qreal的向量,其大小为something个元素,每个元素都将用默认值初始化。

其次,Qt::DashLine是解析为2enum值,因此线QVector<qreal>(ePenStyl)创建2个qreals的向量,其值将为0。

第三,setDashPattern并不像你想象的那样工作。这是来自医生的一句话:

将此笔的划线图案设置为给定的图案。这会将笔的样式隐式转换为Qt::CustomDashLine。

模式必须指定为偶数个正条目,其中条目1、3、5…是短划线,条目2、4、6…是空格。

我想你想做的是

noneSelectedLine->setStyle( ePenStyl );

而不是

noneSelectedLine->setDashPattern(QVector<qreal>(ePenStyl));