Klaus Schneider | 30 Apr 16:02
Picon

QwtScaleWidget has wrong scale

Dear all,

I tried to use a QwtScaleWidget stand-alone for a colour bar scale which I 
want to use independently from QwtPlot. I looked into the spectrogram example 
and the Qwt source code and finally tried the following:

   // interval and colour map (from Spectrogram example)
   QwtDoubleInterval interval(0., 10.);
   QwtLinearColorMap colormap(Qt::darkCyan, Qt::red);
   colormap.addColorStop(0.1, Qt::cyan);
   colormap.addColorStop(0.6, Qt::green);
   colormap.addColorStop(0.95, Qt::yellow);
   // Qwt scale widget and stuff
   QwtScaleWidget* scale = new QwtScaleWidget(QwtScaleDraw::RightScale, this);
   QwtLinearScaleEngine se;
   scale->setScaleDiv(se.transformation(),
              se.divideScale(interval.minValue(), interval.maxValue(), 8, 
5)); // as in QwtPlot::Axis
   scale->setColorBarEnabled(true);
   scale->setColorMap(interval, colormap);
   scale->setTitle("Intensity");
   scale->setMargin(2);

Surprisingly, the scale does not start at the end of the colour bar, but has 
some offset, thus the numbers do not correctly correspond to the colours 
(which is, however, correct in the spectrogram example, where a QwtPlot axis 
is used); see attached screen shot of my test application. What have I done 
wrong?

Thanks a lot,
(Continue reading)

Rainer Thaden | 30 Apr 16:37
Picon

Re: QwtScaleWidget has wrong scale

Hi Klaus,
> 
> I tried to use a QwtScaleWidget stand-alone for a colour bar scale which I 
> want to use independently from QwtPlot. I looked into the spectrogram example 
> and the Qwt source code and finally tried the following:
> 
>... 
> Surprisingly, the scale does not start at the end of the colour bar, but has 
> some offset, thus the numbers do not correctly correspond to the colours 
> (which is, however, correct in the spectrogram example, where a QwtPlot axis 
> is used); see attached screen shot of my test application. What have I done 
> wrong?

I guess the problem is that the scale reserves some space for the 
numbers which would otherwise go out of the frame on top and at the 
bottom. I had the same problem with a Thermo. You can ask for that 
space. I did it like this: Derived my own version of a Thermo (or 
colourbar in your case) and added a method scaleMargin as follows

	int scaleMargin() const
	{
		int y1,y2;
		scaleDraw()->getBorderDistHint(font(), y1, y2);
		int y = qMax(y1,y2);
		return y;
	}

Then,

         thermoLayout = new QVBoxLayout();
(Continue reading)

Rainer Thaden | 30 Apr 17:46
Picon

Re: QwtScaleWidget has wrong scale

Hi again,

maybe this one is a bit hard to understand and differs in your case:

> 
> Then,
> 
>          thermoLayout = new QVBoxLayout();
> 	int margin=thermoLevel->scaleMargin();
>          thermoLevel->setMinimumSize(QSize(48, 170-2*margin));
> 	thermoLayout->setContentsMargins(0, margin, 0, margin);
> 	thermoLayout->addWidget(thermoLevel);
> 	gridLayout->addLayout(thermoLayout, 1, 1, 1, 1);
> 
> 
> Thus, you shrink the colour-bar to fit the scale.
> 

It means, add a Layout around the thermo (colourbar) and set the top and 
bottom margins to the same values as the scale reserves for the numbers.
In your case, you have to ask your free standing scale for its margins 
and apply them to the VBox the colourbar is in.
So, the colourbar is shrunk to the same size as your actual scale.

Hope, it's getting clear, now.

Regards,

Rainer

(Continue reading)

Klaus Schneider | 30 Apr 18:58
Picon

Re: QwtScaleWidget has wrong scale

Hi,

thanks for your suggestion. I tried it but it didn't work. Still I haven't 
quite understood your solution, though: How can the VBox change the internal 
behaviour of the colour scale, which is one widget with one bounding 
rectange? Can you see an error in my implementation below, or does it simply 
not work with a colour scale?

I tried:

class QwtScaleWidgetX : public QwtScaleWidget
{
Q_OBJECT

public:
   explicit QwtScaleWidgetX(QwtScaleDraw::Alignment alignment, QWidget *parent 
= NULL)
     : QwtScaleWidget(alignment, parent)
   {}
   int scaleMargin() const
   {
      int y1,y2;
      scaleDraw()->getBorderDistHint(font(), y1, y2);
      int y = qMax(y1,y2);
      return y;
   }
};

...

(Continue reading)

Rainer Thaden | 1 May 20:43
Picon

Re: QwtScaleWidget has wrong scale

Hi Klaus,
> Hi,
> 
> thanks for your suggestion. I tried it but it didn't work. Still I haven't 
> quite understood your solution, though: How can the VBox change the internal 
> behaviour of the colour scale, which is one widget with one bounding 
> rectange? Can you see an error in my implementation below, or does it simply 
> not work with a colour scale?

If you have the scale and the colour bar in the same layout, then, the 
scale line will fill the height of the layout minus the space needed for 
the numbers above and below the line.
The colour bar, however, will take the whole height and, thus, is longer 
than the line of the scale. So, you'll have to shrink the colour bar.
How can you do that? By putting it in an own layout and set the layout 
margins to the same value as the margins of the scale which are reserved 
  for the numbers.
You, then, shrink the colour bar while keeping the length of the scale 
constant.

>    // Qwt scale widget and stuff
>    QwtScaleWidgetX* scale = new QwtScaleWidgetX(QwtScaleDraw::RightScale, 
> this);
>    QwtLinearScaleEngine se;
>    scale->setScaleDiv(se.transformation(),
>                       se.divideScale(interval.minValue(), interval.maxValue(), 
> 8, 5)); // as in QwtPlot::Axis
>    scale->setColorBarEnabled(true);
>    scale->setColorMap(interval, colormap);
>    scale->setTitle("Intensity");
(Continue reading)


Gmane