#include <QtGui/QApplication>
#include <QMainWindow>
#include <QPainter>
#include <QPen>
#include <qwt_plot.h>
#include <qwt_scale_draw.h>
#include <qwt_text.h>
#include <qwt_painter.h>
#include <qwt_math.h>
#if QT_VERSION < 0x040000
#include <qwmatrix.h>
#define QwtMatrix QWMatrix
#else
#include <qmatrix.h>
#define QwtMatrix QMatrix
#endif
class MyScaleDraw
{
public:
MyScaleDraw() {}
protected:
virtual void drawLabel
( QPainter * painter,
double value
) const {
QwtText lbl
= tickLabel
(painter
->font
(), value
);
if ( lbl.isEmpty() )
return;
lbl.
setBackgroundPen( QPen( Qt
::black ) );
QPoint pos
= labelPosition
(value
);
QSize labelSize
= lbl.
textSize(painter
->font
());
if ( labelSize.height() % 2 )
labelSize.setHeight(labelSize.height() + 1);
labelSize = metricsMap.layoutToDevice(labelSize);
pos = metricsMap.layoutToDevice(pos);
const QwtMatrix m = labelMatrix( pos, labelSize);
painter->save();
#if QT_VERSION < 0x040000
painter->setWorldMatrix(m, true);
#else
painter->setMatrix(m, true);
#endif
QwtPainter::setMetricsMap(metricsMap
);
// restore metrics map
painter->restore();
}
private:
};
int main(int argc, char *argv[])
{
pPlot
->setAxisScale
( QwtPlot::yLeft,
0.0, 1.0e8, 5.0e6
);
pPlot
->setAxisScaleDraw
( QwtPlot::yLeft,
new MyScaleDraw
() );
w.setCentralWidget( pPlot );
w.resize( 500, 500 );
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include <QMainWindow>
#include <QPainter>
#include <QPen>
#include <qwt_plot.h>
#include <qwt_scale_draw.h>
#include <qwt_text.h>
#include <qwt_painter.h>
#include <qwt_math.h>
#if QT_VERSION < 0x040000
#include <qwmatrix.h>
#define QwtMatrix QWMatrix
#else
#include <qmatrix.h>
#define QwtMatrix QMatrix
#endif
class MyScaleDraw
: public QwtScaleDraw
{
public:
MyScaleDraw() {}
protected:
virtual void drawLabel( QPainter * painter, double value ) const
{
QwtText lbl = tickLabel(painter->font(), value);
if ( lbl.isEmpty() )
return;
lbl.setBackgroundPen( QPen( Qt::black ) );
QPoint pos = labelPosition(value);
QSize labelSize = lbl.textSize(painter->font());
if ( labelSize.height() % 2 )
labelSize.setHeight(labelSize.height() + 1);
const QwtMetricsMap metricsMap = QwtPainter::metricsMap();
QwtPainter::resetMetricsMap();
labelSize = metricsMap.layoutToDevice(labelSize);
pos = metricsMap.layoutToDevice(pos);
const QwtMatrix m = labelMatrix( pos, labelSize);
painter->save();
#if QT_VERSION < 0x040000
painter->setWorldMatrix(m, true);
#else
painter->setMatrix(m, true);
#endif
lbl.draw (painter, QRect(QPoint(0, 0), labelSize) );
QwtPainter::setMetricsMap(metricsMap); // restore metrics map
painter->restore();
}
private:
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QwtPlot * pPlot = new QwtPlot;
pPlot->setAxisScale( QwtPlot::yLeft, 0.0, 1.0e8, 5.0e6 );
pPlot->setAxisScaleDraw( QwtPlot::yLeft, new MyScaleDraw() );
QMainWindow w;
w.setCentralWidget( pPlot );
w.resize( 500, 500 );
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks