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
Here the cpp file
Code:
myAxisDraw
::myAxisDraw(QColor c
){
painter->setPen(c);
draw(painter, palette);
}
And here is the function to change de color
Code:
void myClass::myFunction()
{
myAxisDraw * a = new myAxisDraw(c);
myPlot->setAxisScaleDraw(myPlot->xBottom, a );
myPlot->replot();
}
It compiles but nothing happends. Any idea?
Thanks in advance!!
Re: Change the color of the qwt plot axis, numbers and ticks
Quote:
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
Re: Change the color of the qwt plot axis, numbers and ticks
I tried to understand it, I tried:
Code:
palette.
setColor(QPalette::WindowText, c
);
because this option is related to the Forground Color in the QPalette class. But nothing happens.
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
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:
Code:
myScale = myPlot->axisScaleDraw(myPlot->xBottom);
palette.
setColor(QPalette::WindowText, c
);
// Now I can not do that
myScale->draw(painter, palette);
Do you understand the idea?
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
Re: Change the color of the qwt plot axis, numbers and ticks
Quote:
Originally Posted by
bigjoeystud
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
Code:
myplot
->axisWidget
(QwtPlot::xBottom)->setFont
(myFont
);
But I dont know how to change the numbers color.
Thanks!!!
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:
Code:
{
public:
ScaleDraw ();
virtual ~ScaleDraw () {};
virtual QwtText label
(double value
) const {
ret_val.setColor (_color);
return ret_val;
};
private:
};
Joey
Re: Change the color of the qwt plot axis, numbers and ticks
Quote:
Originally Posted by
bigjoeystud
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:
Code:
{
public:
ScaleDraw ();
virtual ~ScaleDraw () {};
virtual QwtText label
(double value
) const {
ret_val.setColor (_color);
return ret_val;
};
private:
};
Joey
Hi again, I think I triyed that solution before with no result.
Y create a similar code
Code:
{
public:
NewDraw
(QColor c
) {my_color
= c;
};
virtual QwtText label
(double value
) const {
ret_val.setColor (my_color);
return ret_val;
};
private:
};
And when I want to chande de color:
Code:
my_Plot
->setAxisScaleDraw
(QwtPlot::xBottom,
new NewDraw
(c
));
But it doesnt work. Instead of changing the color of the numbers, all the numbers and ticks disappear. Any idea? Thanx
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
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
Re: Change the color of the qwt plot axis, numbers and ticks
I tried the following and it works.
Code:
palette.
setColor( QPalette::WindowText, Qt
::gray);
// for ticks palette.
setColor( QPalette::Text, Qt
::gray);
// for ticks' labels qwtsw->setPalette( palette );
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