2015-11-09 21 views
2

正如我的片段中的註釋媒體鏈接解釋說:爲什麼setwidth到其他然後1使我的線消失?

如預期的筆線設置爲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]); 
} 

可以在這裏的是什麼!原因? 如果代碼段中缺少一些信息,請告訴我,我會澄清。

回答

2

您的代碼存在多個問題。首先,矢量構造函數QVector<qreal>(something)的這個超載創建了一個尺寸爲something元素的qreal矢量,每個元素都將使用默認值進行初始化。

其次,Qt::DashLineenum值解析爲2,太行QVector<qreal>(ePenStyl)創造的2 qreal秒的載體,它的值是0

三,setDashPattern不工作的方式,你認爲它有效。這是一個報價from the doc

將此筆的破折號模式設置爲給定模式。這隱式轉換筆的風格爲Qt :: CustomDashLine。

該模式必須指定爲偶數個正項,其中條目1,3,5 ...是破折號,2,4,6 ...是空格。

我想你想要做的是

noneSelectedLine->setStyle(ePenStyl); 

,而不是

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

我會檢查,感謝這麼遠。 – dhein

相關問題