Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Change colour of specific item in Qwt Plot



  • Hi All,

    I have developed a qwt that contains multiple QwtPlotShapeItems.

    //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:

    //.....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!


Log in to reply