Results 1 to 13 of 13

Thread: Change the color of the qwt plot axis, numbers and ticks

  1. #1
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Change the color of the qwt plot axis, numbers and ticks

    Hi,

    I'm triying to change the color of the numbers and axis of a Qwt plot. I think there is no simple way to do it (like myplot->setAxisColor(Axis id, QColor c).

    So I tried to create a subclass of a QwtScaleDraw, and set the color to it. Then, set this ScaleDraw to my plot.

    Here is de code:

    Here is my subclass header file
    Qt Code:
    1. class myAxisDraw: public QwtScaleDraw
    2. {
    3. public:
    4. myAxisDraw(QColor c);
    5. };
    To copy to clipboard, switch view to plain text mode 

    Here the cpp file
    Qt Code:
    1. myAxisDraw::myAxisDraw(QColor c)
    2. {
    3. QPalette palette(c);
    4. QPainter * painter = new QPainter;
    5. painter->setPen(c);
    6. draw(painter, palette);
    7. }
    To copy to clipboard, switch view to plain text mode 

    And here is the function to change de color
    Qt Code:
    1. void myClass::myFunction()
    2. {
    3. QColor c = QColorDialog::getColor( Qt::white, this );
    4. myAxisDraw * a = new myAxisDraw(c);
    5. myPlot->setAxisScaleDraw(myPlot->xBottom, a );
    6. myPlot->replot();
    7. }
    To copy to clipboard, switch view to plain text mode 

    It compiles but nothing happends. Any idea?

    Thanks in advance!!

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

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    I'm triying to change the color of the numbers and axis of a Qwt plot.
    The axis widget is a QWidget with a QPalette. All labels are painted with the Text color, ticks and backbone with the Foreground color.

    But each label could also have its individual color by overloading QwtScaleDraw::label(double value), because each QwtText can have its own color.

    Uwe

  3. #3
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    I tried to understand it, I tried:

    Qt Code:
    1. QPalette palette;
    2. palette.setColor(QPalette::WindowText, c);
    To copy to clipboard, switch view to plain text mode 
    because this option is related to the Forground Color in the QPalette class. But nothing happens.

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

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    Assigning a color to a local palette object on the stack does nothing. Of course you have to assign the palette to the axisWidgets.

    Uwe

  5. #5
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    I cannot find the class QAxisWidget or QwtAxisWidget or something like that, I only found QwtScaleWidget and QwtScaleDraw.

    I thought that the last one could be a usfeul class due to the draw method that it has. Every time I need to change the color of the axis, then I create a new class that is subclass from QwtSCaleDraw, then I call draw inside this class whit local variables of QPainter and QPalette. So the new colors should be set on this class. Then I assging this QwtScaleDraw subclass to my axis.

    There is another important thing, the draw method is protected. So I can not do that:
    Qt Code:
    1. QwtScaleDraw * myScale;
    2.  
    3.  
    4. myScale = myPlot->axisScaleDraw(myPlot->xBottom);
    5.  
    6. QPalette palette;
    7. palette.setColor(QPalette::WindowText, c);
    8.  
    9. QPainter * p = new QPainter;
    10. // Now I can not do that
    11. myScale->draw(painter, palette);
    To copy to clipboard, switch view to plain text mode 

    Do you understand the idea?

  6. #6
    Join Date
    Sep 2007
    Posts
    61
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    I think what you want is myplot->axisWidget (QwtPlot::xBottom)->setPalette (palette).

    Joey

  7. The following user says thank you to bigjoeystud for this useful post:

    locke (17th May 2010)

  8. #7
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    Quote Originally Posted by bigjoeystud View Post
    I think what you want is myplot->axisWidget (QwtPlot::xBottom)->setPalette (palette).

    Joey
    That was exactly what I wanted!!

    And what about the numbers in the axis? I can change their Font using

    Qt Code:
    1. myplot->axisWidget (QwtPlot::xBottom)->setFont(myFont);
    To copy to clipboard, switch view to plain text mode 

    But I dont know how to change the numbers color.

    Thanks!!!

  9. #8
    Join Date
    Sep 2007
    Posts
    61
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    I believe you have to override QwtScaleDraw with your own QwtScaleDraw::label routine. The label routine returns a QwtText which can have a color. Here's an example:

    Qt Code:
    1. class ScaleDraw: public QwtScaleDraw
    2. {
    3. public:
    4. ScaleDraw ();
    5. virtual ~ScaleDraw () {};
    6.  
    7. virtual QwtText label (double value) const
    8. {
    9. QwtText ret_val = QwtText (QString::number (value));
    10. ret_val.setColor (_color);
    11. return ret_val;
    12. };
    13.  
    14. private:
    15. QColor _color;
    16. };
    To copy to clipboard, switch view to plain text mode 

    Joey

  10. The following user says thank you to bigjoeystud for this useful post:

    locke (17th May 2010)

  11. #9
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    Quote Originally Posted by bigjoeystud View Post
    I believe you have to override QwtScaleDraw with your own QwtScaleDraw::label routine. The label routine returns a QwtText which can have a color. Here's an example:

    Qt Code:
    1. class ScaleDraw: public QwtScaleDraw
    2. {
    3. public:
    4. ScaleDraw ();
    5. virtual ~ScaleDraw () {};
    6.  
    7. virtual QwtText label (double value) const
    8. {
    9. QwtText ret_val = QwtText (QString::number (value));
    10. ret_val.setColor (_color);
    11. return ret_val;
    12. };
    13.  
    14. private:
    15. QColor _color;
    16. };
    To copy to clipboard, switch view to plain text mode 

    Joey
    Hi again, I think I triyed that solution before with no result.

    Y create a similar code
    Qt Code:
    1. class NewDraw: public QwtScaleDraw
    2. {
    3. public:
    4. NewDraw(QColor c) {my_color = c;};
    5. virtual QwtText label (double value) const
    6. {
    7. QwtText ret_val = QwtText (QString::number (value));
    8. ret_val.setColor (my_color);
    9. return ret_val;
    10. };
    11.  
    12. private:
    13. QColor my_color;
    14. };
    To copy to clipboard, switch view to plain text mode 

    And when I want to chande de color:
    Qt Code:
    1. QColor c = QColorDialog::getColor( Qt::white, this );
    2.  
    3. my_Plot->setAxisScaleDraw(QwtPlot::xBottom, new NewDraw(c));
    To copy to clipboard, switch view to plain text mode 

    But it doesnt work. Instead of changing the color of the numbers, all the numbers and ticks disappear. Any idea? Thanx

  12. #10
    Join Date
    Sep 2007
    Posts
    61
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    The only time I've seen this is when you are putting things on the stack instead of allocating on the heap. However, if you are doing a new, that's probably right... Your code is virtually identical to mine so I'm at a loss. The only other thing I do differently is also set a font right before setting the color, but I doubt that has anything to do with it. You are only setting the AxisScaleDraw once, right?

    Joey

  13. The following user says thank you to bigjoeystud for this useful post:

    locke (28th May 2010)

  14. #11
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    Yes, only once. I cannot find the solution and there is no much more threads related to this problem

  15. #12
    Join Date
    Mar 2011
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    I tried the following and it works.

    Qt Code:
    1. QwtScaleWidget *qwtsw = myqwtplot.axisWidget(QwtPlot::xBottom);
    2. QPalette palette = qwtsw->palette();
    3. palette.setColor( QPalette::WindowText, Qt::gray); // for ticks
    4. palette.setColor( QPalette::Text, Qt::gray); // for ticks' labels
    5. qwtsw->setPalette( palette );
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Change the color of the qwt plot axis, numbers and ticks

    The tick labels are painted with QPalette::Text from the palette of the scale widget.

    You could also reimplement YourScaleDraw::label(double ) and return a QwtText object with a color - f.e if you want have different fonts/colors for tick labels of the same scale.

    Uwe

Similar Threads

  1. Replies: 1
    Last Post: 16th March 2010, 16:23
  2. Replies: 1
    Last Post: 7th December 2009, 15:59
  3. Axis yLeft cuts of numbers
    By sun in forum Qwt
    Replies: 17
    Last Post: 9th October 2009, 22:36
  4. Replies: 2
    Last Post: 30th June 2009, 18:08
  5. qwp plot axis scale
    By Cal in forum Qwt
    Replies: 1
    Last Post: 11th May 2009, 18:10

Tags for this Thread

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.