Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set different colors?

How to set different colors?

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 3 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    n-2204
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TheGringerEye
      wrote on last edited by
      #2

      Create a new line and set each setPen

      I try to learn English.

      1 Reply Last reply
      1
      • N Offline
        N Offline
        n-2204
        wrote on last edited by
        #3

        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

        jsulmJ 1 Reply Last reply
        0
        • N n-2204

          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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @n-2204 said in How to set different colors?:

          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.
          So, please explain better...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • N Offline
            N Offline
            n-2204
            wrote on last edited by
            #5

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

            jsulmJ 1 Reply Last reply
            0
            • N n-2204

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @n-2204 said in How to set different colors?:

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

              You're already setting color:

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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                n-2204
                wrote on last edited by n-2204
                #7

                @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

                jsulmJ 1 Reply Last reply
                0
                • N n-2204

                  @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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    n-2204
                    wrote on last edited by
                    #9

                    CHPlotCurve-Class to draw the data as a line curve

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      n-2204
                      wrote on last edited by
                      #10

                      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

                      jsulmJ 1 Reply Last reply
                      0
                      • N n-2204

                        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

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @n-2204 said in How to set different colors?:

                        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)...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          n-2204
                          wrote on last edited by n-2204
                          #12

                          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

                          jsulmJ 1 Reply Last reply
                          0
                          • N n-2204

                            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

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @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.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            N 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @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.

                              N Offline
                              N Offline
                              n-2204
                              wrote on last edited by
                              #14

                              @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??

                              jsulmJ 1 Reply Last reply
                              0
                              • N n-2204

                                @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??

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

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

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                N 1 Reply Last reply
                                0
                                • jsulmJ jsulm

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

                                  N Offline
                                  N Offline
                                  n-2204
                                  wrote on last edited by
                                  #16

                                  @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

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • N n-2204

                                    @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

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @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).

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    N 1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @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).

                                      N Offline
                                      N Offline
                                      n-2204
                                      wrote on last edited by
                                      #18

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

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • N n-2204

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

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @n-2204 said in How to set different colors?:

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

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        0
                                        • N Offline
                                          N Offline
                                          n-2204
                                          wrote on last edited by n-2204
                                          #20

                                          @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 ?
                                          Thanks for your answers.
                                          every time i take point ..create line data and pass to line
                                          CHPlotCurveData* linedata1 = new CHPlotCurveData();
                                          CHPlotCurve* line = (CHPlotCurve*)pGr->insertCurve("line", CHPlotGraph2D::Line, false);

                                          jsulmJ 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved