#include <qwt_plot.h>
enum class ScaleType
{
Linear,
Logarithmic
};
{
public:
Plot();
void setScaleType(ScaleType scaleType);
private:
ScaleType m_scaleType = ScaleType::Linear;
void createScaleItem();
};
#include <qwt_plot.h>
enum class ScaleType
{
Linear,
Logarithmic
};
class QwtPlotScaleItem;
class Plot : public QwtPlot
{
public:
Plot();
void setScaleType(ScaleType scaleType);
private:
ScaleType m_scaleType = ScaleType::Linear;
QwtPlotScaleItem* m_scaleItem = nullptr;
void createScaleItem();
};
To copy to clipboard, switch view to plain text mode
#include "Plot.h"
#include <QPen>
#include <qwt_plot_grid.h>
#include <qwt_plot_opengl_canvas.h>
#include <qwt_scale_engine.h>
#include <qwt_scale_widget.h>
#include <qwt_plot_scaleitem.h>
{
createScaleItem();
m_scaleItem->attach(this);
setCanvas(new QwtPlotOpenGLCanvas());
grid->enableXMin(true);
grid->enableYMin(true);
grid
->setMajorPen
(QPen(Qt
::black,
0, Qt
::DotLine));
grid
->setMinorPen
(QPen(Qt
::gray,
0, Qt
::DotLine));
grid->attach(this);
setAxisAutoScale
(QwtPlot::xBottom,
true);
}
void Plot::setScaleType(ScaleType scaleType)
{
if (m_scaleType == scaleType)
{
return;
}
m_scaleType = scaleType;
auto* scaleEngine = m_scaleType == ScaleType::Linear ?
static_cast<QwtScaleEngine*>(new QwtLogScaleEngine);
const bool isFloatingEnabled
= axisScaleEngine
(QwtPlot::xBottom)->testAttribute
(QwtScaleEngine::Floating);
setAxisScaleEngine
(QwtPlot::xBottom, scaleEngine
);
replot();
}
void Plot::createScaleItem()
{
m_scaleItem
->setXAxis
(QwtPlot::xBottom);
m_scaleItem->setScaleDivFromAxis(true);
m_scaleItem
->setFont
(axisWidget
(QwtPlot::xBottom)->font
());
m_scaleItem->setBorderDistance(0);
auto* scaleDraw = m_scaleItem->scaleDraw();
}
#include "Plot.h"
#include <QPen>
#include <qwt_plot_grid.h>
#include <qwt_plot_opengl_canvas.h>
#include <qwt_scale_engine.h>
#include <qwt_scale_widget.h>
#include <qwt_plot_scaleitem.h>
Plot::Plot() : QwtPlot()
{
axisWidget(QwtPlot::xBottom)->scaleDraw()->enableComponent(QwtAbstractScaleDraw::Ticks, false);
axisWidget(QwtPlot::xBottom)->scaleDraw()->enableComponent(QwtAbstractScaleDraw::Backbone, false);
createScaleItem();
m_scaleItem->attach(this);
setCanvas(new QwtPlotOpenGLCanvas());
auto* grid = new QwtPlotGrid();
grid->enableXMin(true);
grid->enableYMin(true);
grid->setMajorPen(QPen(Qt::black, 0, Qt::DotLine));
grid->setMinorPen(QPen(Qt::gray, 0, Qt::DotLine));
grid->attach(this);
setAxisAutoScale(QwtPlot::xBottom, true);
}
void Plot::setScaleType(ScaleType scaleType)
{
if (m_scaleType == scaleType)
{
return;
}
m_scaleType = scaleType;
auto* scaleEngine = m_scaleType == ScaleType::Linear ?
static_cast<QwtScaleEngine*>(new QwtLinearScaleEngine) :
static_cast<QwtScaleEngine*>(new QwtLogScaleEngine);
const bool isFloatingEnabled = axisScaleEngine(QwtPlot::xBottom)->testAttribute(QwtScaleEngine::Floating);
setAxisScaleEngine(QwtPlot::xBottom, scaleEngine);
axisScaleEngine(QwtPlot::xBottom)->setAttribute(QwtScaleEngine::Floating, isFloatingEnabled);
replot();
}
void Plot::createScaleItem()
{
m_scaleItem = new QwtPlotScaleItem();
m_scaleItem->setAlignment(QwtScaleDraw::TopScale);
m_scaleItem->setXAxis(QwtPlot::xBottom);
m_scaleItem->setScaleDivFromAxis(true);
m_scaleItem->setFont(axisWidget(QwtPlot::xBottom)->font());
m_scaleItem->setBorderDistance(0);
auto* scaleDraw = m_scaleItem->scaleDraw();
scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
scaleDraw->enableComponent(QwtScaleDraw::Labels, false);
scaleDraw->enableComponent(QwtScaleDraw::Ticks, true);
}
To copy to clipboard, switch view to plain text mode
#include "QwtPlotClient.h"
#include "Plot.h"
#include <qwt_plot_curve.h>
#include <qwt_symbol.h>
#include <QPushButton>
#include <QSpacerItem>
namespace
{
const QVector<QPointF> linCurvePoints{ {-10, 10}, {20, 0}, {30, 20} , {40, -10} , {50, -5} };
const QVector<QPointF> logCurvePoints{ {20, 0}, {30, 20} , {40, -10} , {50, -5} };
}
QwtPlotClient
::QwtPlotClient(QWidget* parent
) m_plot(new Plot()),
{
m_ui.setupUi(this);
m_ui.plotFrame->layout()->addWidget(m_plot);
m_curve->setSamples(linCurvePoints);
m_curve->setPen(Qt::red);
m_curve->attach(m_plot);
m_ui.verticalLayout->addLayout(buttonLayout);
connect(button, &QPushButton::clicked, this, [this]()
{
m_curve->setSamples(linCurvePoints);
m_plot->setScaleType(ScaleType::Linear);
});
buttonLayout->addWidget(button);
connect(button, &QPushButton::clicked, this, [this]()
{
m_curve->setSamples(logCurvePoints);
m_plot->setScaleType(ScaleType::Logarithmic);
});
buttonLayout->addWidget(button);
}
#include "QwtPlotClient.h"
#include "Plot.h"
#include <qwt_plot_curve.h>
#include <qwt_symbol.h>
#include <QPushButton>
#include <QSpacerItem>
namespace
{
const QVector<QPointF> linCurvePoints{ {-10, 10}, {20, 0}, {30, 20} , {40, -10} , {50, -5} };
const QVector<QPointF> logCurvePoints{ {20, 0}, {30, 20} , {40, -10} , {50, -5} };
}
QwtPlotClient::QwtPlotClient(QWidget* parent)
: QDialog(parent),
m_plot(new Plot()),
m_curve(new QwtPlotCurve)
{
m_ui.setupUi(this);
m_ui.plotFrame->layout()->addWidget(m_plot);
m_curve->setSamples(linCurvePoints);
m_curve->setPen(Qt::red);
m_curve->setSymbol(new QwtSymbol(QwtSymbol::Ellipse, QBrush(Qt::red), QPen(Qt::darkRed), QSize(8, 8)));
m_curve->attach(m_plot);
auto* buttonLayout = new QHBoxLayout;
m_ui.verticalLayout->addLayout(buttonLayout);
auto button = new QPushButton("Linear");
connect(button, &QPushButton::clicked, this, [this]()
{
m_curve->setSamples(linCurvePoints);
m_plot->setScaleType(ScaleType::Linear);
});
buttonLayout->addWidget(button);
button = new QPushButton("Log");
connect(button, &QPushButton::clicked, this, [this]()
{
m_curve->setSamples(logCurvePoints);
m_plot->setScaleType(ScaleType::Logarithmic);
});
buttonLayout->addWidget(button);
buttonLayout->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Fixed));
}
To copy to clipboard, switch view to plain text mode
Bookmarks