PDA

View Full Version : How to use explicit labels/ticks?



ssample
16th January 2013, 19:22
Hi, I have very newbie-style problem.
I've written this code:



#include "qwt_plot.h"
#include "qwt_scale_div.h"
#include <QApplication>

int main(int argc, char ** argv) {
QApplication app(argc, argv);

QwtPlot plot;
plot.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
plot.setCanvasBackground(Qt::white);
plot.setAxisScale(QwtPlot::yLeft, 0, 50);

QList<double> ticks = QList<double>()
<< 10
<< 40
<< 90;
QwtScaleDiv pressScaleDiv;
pressScaleDiv.setTicks(QwtScaleDiv::MajorTick, ticks);
plot.setAxisScaleDiv(QwtPlot::yLeft, pressScaleDiv);

plot.show();
return app.exec();

}


My wish is, that y axis has ticks and labels at values 10, 40, 90.
It seems like code above gives no effect - labels are at 0, 10, 20, 30, 40, 50 + intermediate ticks.
What am I doing wrong?
I suspect that the error is trivial :o ....

Uwe
17th January 2013, 06:44
A QwtScaleDiv consists of the range of a scale and the positions of its ticks. Your code creates an invalid QwtScaleDiv object for 2 reasons:



it doesn't have a range
QwtScaleDiv::isValid() is only true, when using the constructor with a valid range



The second reason is left over from the very early Qwt versions - it should have never been like this. That's why the isValid() flag has been removed in Qwt 6.1.

Uwe

PS: The line with setAxisScale() is pointless, when you assign a different scale a couple of lines below

ssample
17th January 2013, 20:25
Thanks, Uwe.
I've improved my wonderful code:



#include "qwt_plot.h"
#include "qwt_scale_div.h"
#include <QApplication>

int main(int argc, char ** argv) {
QApplication app(argc, argv);

QwtPlot plot;
plot.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
plot.setCanvasBackground(Qt::white);


QList<double> ticks[QwtScaleDiv::NTickTypes] = {
QList<double>(),
QList<double>(),
QList<double>()
<< 10
<< 40
<< 90
};
QwtScaleDiv pressScaleDiv(0, 100, ticks);

// QwtScaleDiv pressScaleDiv;
// pressScaleDiv.setInterval(0, 100);
// pressScaleDiv.setTicks(
// QwtScaleDiv::MinorMajor,
// QList<double>()
// << 10
// << 40
// << 90
// );
plot.setAxisScaleDiv(QwtPlot::yLeft, pressScaleDiv);

plot.show();
return app.exec();

}


and it works - but when I use the commented section instead of the working one above - it doesn't. No problem for me, anyway.
Thanks a lot.

Uwe
18th January 2013, 11:16
but when I use the commented section instead of the working one above - it doesn't.
That's because of what I wrote as 2.). The code would be o.k. for Qwt 6.1, but all earlier versions ( starting with Qwt 0.1 ) have this nasty QwtScaleDiv default constructor.

Uwe