Results 1 to 3 of 3

Thread: Change colour of specific item in Qwt Plot

  1. #1

    Default Change colour of specific item in Qwt Plot

    Hi All,

    I have developed a qwt that contains multiple QwtPlotShapeItems.

    Qt Code:
    1. //Run a function to get an array of polygons
    2. QVector<QPolygonF> polygonArray;
    3. createPolygonArray(lines, polygonArray, maxdistance);
    4.  
    5. //add each polygon in the polygon array to the qwt plot
    6. for (int j = 0; j < polygonArray.size(); j = j + 1)
    7. {
    8. QwtPlotShapeItem *shape = new QwtPlotShapeItem;
    9. shape->setPen(QColor(0,0,0), Qt::SolidLine);
    10. shape->setTitle("shapeNumber_" + QString::number(j));
    11. shape->setBrush(QColor(130,130,130,50));
    12. shape->setPolygon(polygonArray[j]);
    13. shape->attach(&myPlot);
    14. V.push_back(shape);
    15. }
    16. myPlot.replot();
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. //.....later on in my code, in another function that the plot is passed into (by ref)
    2. for (int j = 0; j < myPlot.numberOfAttachedShapeItems(); j = j + 1)
    3. {
    4. if (myPlot.attachedShapeItem[j].Title() == "shapeNumber_7")
    5. {
    6. myPlot.attachedShapeItem[j].setBrush(QColor(0,0,0,255));
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,241
    Thanked 860 Times in 809 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Change colour of specific item in Qwt Plot

    Quote Originally Posted by AhmedAlshawi View Post
    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

  3. #3

    Default Re: Change colour of specific item in Qwt Plot

    Quote Originally Posted by Uwe View Post
    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!

    Qt Code:
    1. QwtPlotItemList plotItemList = myPlot.itemList();
    2. for (int m = 0; m < plotItemList.size(); m = m + 1)
    3. {
    4. if(plotItemList[m]->title().text() == QString::fromStdString(desiredName[i]))
    5. {
    6. plotItemList[m]-> //??? there is no setBrush to use here....I would expect to write...setBrush(QColor(0,0,0,255));
    7. }
    8. else
    9. {
    10. plotItemList[m]-> //??? there is no setBrush to use here....I would expect to write...setBrush(QColor(130,130,130,50));
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 


    Added after 30 minutes:


    Quote Originally Posted by Uwe View Post
    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).

    Qt Code:
    1. QwtPlotItemList plotItemList = myPlot.itemList(QwtPlotItem::Rtti_PlotShape /*added this*/);
    2. for (int m = 0; m < plotItemList.size(); m = m + 1)
    3. {
    4. QwtPlotShapeItem *shape = static_cast<QwtPlotShapeItem *>( plotItemList[m] ); //added this line
    5.  
    6. if(plotItemList[m]->title().text() == QString::fromStdString(desiredName[i]))
    7. {
    8. shape->setBrush(QColor(0,0,0,255)); //now can set brush to the shape directly
    9. }
    10. else
    11. {
    12. shape->setBrush(QColor(130,130,130,50)); //now can set brush to the shape directly
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by AhmedAlshawi; 19th February 2020 at 11:21.

Similar Threads

  1. Change the colour of a table view row
    By Splatify in forum Newbie
    Replies: 0
    Last Post: 7th March 2011, 06:31
  2. Change the colour of a QT TextEdit row
    By Splatify in forum Newbie
    Replies: 3
    Last Post: 3rd March 2011, 14:09
  3. change the text colour in Menubar
    By chinmayapadhi in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2010, 01:31
  4. How to change Text colour in EditText?
    By phillip_Qt in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2008, 08:48
  5. Change colour of QString
    By Morea in forum Newbie
    Replies: 9
    Last Post: 10th February 2006, 22:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.