Automatic update of scale division on resize of plot widget
Hi,
I'm having the following problem:
When I resize my application window (which contains a QwtPlot) I would like the scale division to be updated according to the new dimensions. This means that, e.g if the window size is increased, the proper scale division is recalculated and new major ticks are introduced because of the newly created "free space" between the ticks. And of course, vice versa if one shrinks the window size.
Please bear with me if I am missing something obvious :)
Many thanks for any help :)
Re: Automatic update of scale division on resize of plot widget
See QwtPlotRescaler and the navigation example.
Uwe
Re: Automatic update of scale division on resize of plot widget
Thanks for pointing me to the navigation example. I read the code and compiled it, but none of the available resize policies does what I want:
If the limits of one scale are fixed (for example the y axis while "fitting" is selected), the scale division does not change and the major ticks stay the same during resizing the window. In case that the limits of the scales are not fixed (for example for both axes while "expanding" is selected) the scale division changes and the major ticks are set appropriately, but I don't want the limits of the scales to change during the resize event.
What I would like to yield is that the limits of both axes stay exactly the same while resizing the plot (as it is the case without using a QwtPlotRescaler) but the number of major (and minor) ticks within these fixed limits should be increased when the plot is stretched (as there is more space for ticks and labels) and decreased when the plot is shrunk (since there is less space for ticks and labels).
Is there a way to do this?
2 Attachment(s)
Re: Automatic update of scale division on resize of plot widget
The description of my problem might be a little bit confusing, therefore I will illustrate it with two screenshots.
In the first picture, my application window is very small and the number of major ticks (0,5,10,15) seems well chosen and the available screen space is filled optimal with the ticks.
In the second picture, I resized the very same window to the full width of my screen. My problem is, that the number of ticks and labels is still exactly the same as in the small window. The spacing between the ticks is now very big and does not seem well chosen to me. There is enough space to add more major (and minor) ticks (lets say -2, 0,2,4,6,8 and so on), which would result in a much more adequate looking axis.
Attachment 5161Attachment 5162
Is there a way to recalculate the number of ticks whenever the window gets resized?
Re: Automatic update of scale division on resize of plot widget
Install an eventFilter for the scale widgets and handle the resize events. There you can change the scale division using the regular Qwt APIs. Maybe it's worth to reimplement QwtPlot::minimumSizeHint as it depends on the scale widgets ( and its ticks and labels ) in the first place.
Uwe
2 Attachment(s)
Re: Automatic update of scale division on resize of plot widget
Sorry for reviving this thread after so much time, but I still have difficulties with my implementation.
I successfully installed the eventFilter and could also get hold of the resize event so that I can change the number of ticks depending on the size of the scale. This is my current code:
Code:
{
if (e
->type
() == QEvent::Resize) {
//calculate the maximum amount of ticks
//"scale" is a pointer to the corresponding scaleDraw
qint32 count = scale->length() / scale->maxLabelWidth(preferences::frequencyAxisLabelFont);
count = count / 4; //calculate reasonable amount of ticks
if (count < 3) //always keep minimum number of ticks
count = 3;
setAxisMaxMajor
(QwtPlot::xBottom, count
);
replot();
}
return false;
}
Now the problem: The number of actually displayed ticks changes drastically between minimal changes of the window size. I will illustrate it with 2 screenshots:
Attachment 5428Attachment 5429
The left screenshot shows the window with a specific size. For the right screenshot I increased the window size the smallest amount possible using the mouse. Now I have a considerably larger number of ticks. If I further increase the window size (just about 1 pixel) I again have the same amount of ticks as in the left screenshot, and so on...
This results in a "flickering" of the axis ticks while resizing the window.
I am sure this is caused by my (probably very bad) implementation. But I searched all classes available and found no other way to change the number of ticks.
I would be very happy if someone could propose a suitable way to achieve this.
Thank you very much in advance.