gpt4 book ai didi

c++ - 为什么 setwidth 为 other then 1 会使我的行消失?

转载 作者:搜寻专家 更新时间:2023-10-31 02:19:19 25 4
gpt4 key购买 nike

正如我的代码片段中的评论已经解释的那样:

对于笔设置为 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 为 other then 1 会使我的行消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33605885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com