# How to set different colors?

• Hi,,
I am taking x0,y0,x1,y1 from a table input and based on that I'm drawing line between each two point(x0y0 to x1y1)

``````  //code for line draw
for (double i = 0; i <= 1; i++)
{
QPointF pt3(X, Y);
linedata1->append(pt3);
X = x0;
Y = y0;
}
CHPlotCurve* line = (CHPlotCurve*)pGr->insertCurve("line", CHPlotGraph2D::Line, false);
line->setSamples(linedata1);
line->setPen(Qt::blue, 2, Qt::SolidLine)
``````

Here I'm setting color as blue that is applicable for all the lines,, but I need to set different color of every line drawn
How to set that ?

Thank You

• Create a new line and set each setPen

• it got overwrite..
i have data in table for 6 row so there might be from point1-2,2-3,3-4 line drawing like this
so how it knows for first line it color will be this for 2 this like that

• so how it knows for first line it color will be this for 2 this like that

How should we know?!
You did not explain from where the color should come.

• Ok..
I'm also asking like how to set color for each line that will be drawn using table data

• I'm also asking like how to set color for each line that will be drawn using table data

``````line->setPen(Qt::blue, 2, Qt::SolidLine)
``````

Simply set the color you want to set for a specific line. I really fail to understand what the problem is...

• @jsulm ya using this every line get same color..
but i need to set color when 1,2 line drawn , third , fourth
which is based on no of entries in table column x, y

• @n-2204 If your line consists of more than one segment, then you will need to explain how CHPlotCurve works (show its code). How does it draw each segment? You could have something like setColors(QVector<QColor> colors) to specify colors for all segments.

• CHPlotCurve-Class to draw the data as a line curve

• QVector<QColor> colors = { QColor(255,0,0), QColor(0,255,0), QColor(0,0,255),
QColor(0,0,0), QColor(255,255,255), QColor(0,128,64) };
for(int i=0;i<6;i++)
line->setPen(colors[i]);
}
i unable to set diff color..using this code only same color lines drawn..as i value in not incremented when second time line drawn ?
what changes req..?any suggestions

• i unable to set diff color..using this code only same color lines drawn

Of course it can't work this way! And it is not what I suggested! Please read my post once more...
That's why I asked how your class actually draws. You still did not answer this question (I have no idea how CHPlotCurve does the drawing)...

• I'm using these predefined classes..
@jsulm CHPlotCurve -Class to draw the data as a line curve in the graph canvas area(x-y plane).
CHPlotCurve--derived from QwtPlotCurve class
void CHPlotCurve::setSamples(QwtSeriesData<QPointF> *). CHPlotCurveData is derived from QwtSeriesData<QPointF>. This function is part of the one of the base class of CHPlotCurve.
There are two ways to set color is using setBrush or setpen

• @n-2204 If there is no support for different colors for different segments of a line then you can still draw each segment separately as one line.

• @jsulm yes.. line able to draw.. but how i set color for these line.. like line drawn then how can i set a color to that??

• @n-2204 Come on you already set the color! So, what is your question now?

• @jsulm yes.. this color will be same for each line drawn .. but i need to set diff color for 1,2 3 etc lines... thats why i tried using loop

• @n-2204 If you can set color for one line then you can set color for every line. Something like:

``````// Line 1
QPointF pt1(X, Y);
WHAT_EVER_LINE_DATA_IS linedata1;
linedata1->append(pt1);
CHPlotCurve* line1 = (CHPlotCurve*)pGr->insertCurve("line", CHPlotGraph2D::Line, false);
line1->setSamples(linedata1);
line1->setPen(Qt::blue, 2, Qt::SolidLine);

// Line 2
QPointF pt2(X2, Y2);
WHAT_EVER_LINE_DATA_IS linedata2;
linedata2->append(pt2);
CHPlotCurve* line2 = (CHPlotCurve*)pGr->insertCurve("line", CHPlotGraph2D::Line, false);
line2->setSamples(linedata2);
line2->setPen(Qt::red, 2, Qt::SolidLine)
``````

Also, please consider that this CHPlotCurve is not part of Qt, you should consider checking its documentation and ask in a different forum (if there is one).

• @jsulm ya thanks I knw this .. but problem is I'm taking point from table ..i can't do like this

• I'm taking point from table

So what?
Take the points from the table, I don't see how this stops you from doing it the way I suggested...

• @jsulm I have 8 rows in table so i will do 8 times first store every row data then do this ? is this a way ?