Change colour of specific item in Qwt Plot
Hi All,
I have developed a qwt that contains multiple QwtPlotShapeItems.
Code:
//Run a function to get an array of polygons
QVector<QPolygonF> polygonArray;
createPolygonArray(lines, polygonArray, maxdistance);
//add each polygon in the polygon array to the qwt plot
for (int j = 0; j < polygonArray.size(); j = j + 1)
{
QwtPlotShapeItem *shape = new QwtPlotShapeItem;
shape
->setPen
(QColor(0,
0,
0), Qt
::SolidLine);
shape
->setTitle
("shapeNumber_" + QString::number(j
));
shape
->setBrush
(QColor(130,
130,
130,
50));
shape->setPolygon(polygonArray[j]);
shape->attach(&myPlot);
V.push_back(shape);
}
myPlot.replot();
I want to return to my plot at a later point in my code and loop through all the attached QwtPlotShapeItems, and if it's title matches what I looking for, then I want to change the colour of that shape. So for example:
Code:
//.....later on in my code, in another function that the plot is passed into (by ref)
for (int j = 0; j < myPlot.numberOfAttachedShapeItems(); j = j + 1)
{
if (myPlot.attachedShapeItem[j].Title() == "shapeNumber_7")
{
myPlot.
attachedShapeItem[j
].
setBrush(QColor(0,
0,
0,
255));
}
}
Is this possible, or do I have to recreate my plot each time I want to change the color of a single shapeItem?
Thank you!
Re: Change colour of specific item in Qwt Plot
Quote:
Originally Posted by
AhmedAlshawi
Is this possible, or do I have to recreate my plot each time I want to change the color of a single shapeItem?
Changing the brush only is fine.
Uwe
Re: Change colour of specific item in Qwt Plot
Quote:
Originally Posted by
Uwe
Changing the brush only is fine.
Uwe
I cant find how to change the fill colour of the plotitem. Could you see what I missing? Thank you for your help!
Code:
QwtPlotItemList plotItemList = myPlot.itemList();
for (int m = 0; m < plotItemList.size(); m = m + 1)
{
if(plotItemList
[m
]->title
().
text() == QString::fromStdString(desiredName
[i
])) {
plotItemList[m]-> //??? there is no setBrush to use here....I would expect to write...setBrush(QColor(0,0,0,255));
}
else
{
plotItemList[m]-> //??? there is no setBrush to use here....I would expect to write...setBrush(QColor(130,130,130,50));
}
}
Added after 30 minutes:
Quote:
Originally Posted by
Uwe
Changing the brush only is fine.
Uwe
I managed to work out how to do it :) Here is what I did (I found some code online that had your name on it which guided me to the right solution).
Code:
QwtPlotItemList plotItemList
= myPlot.
itemList(QwtPlotItem::Rtti_PlotShape /*added this*/);
for (int m = 0; m < plotItemList.size(); m = m + 1)
{
QwtPlotShapeItem *shape = static_cast<QwtPlotShapeItem *>( plotItemList[m] ); //added this line
if(plotItemList
[m
]->title
().
text() == QString::fromStdString(desiredName
[i
])) {
shape
->setBrush
(QColor(0,
0,
0,
255));
//now can set brush to the shape directly }
else
{
shape
->setBrush
(QColor(130,
130,
130,
50));
//now can set brush to the shape directly }
}