3 Attachment(s)
Problems with QwtPlotScaleItem and switching scale engine
Hi,
we are using a QwtPlotScaleItem to display the axes' ticks inside of the canvas. And in our software the user has the option to switch between a linear and a logarithmic scale for each axis. I compressed our code to a minimum, so the bug is easier to reprocude. First, I derived from QwtPlot and create the QwtPlotScaleItem.
Plot.h
Code:
#include <qwt_plot.h>
enum class ScaleType
{
Linear,
Logarithmic
};
{
public:
Plot();
void setScaleType(ScaleType scaleType);
private:
ScaleType m_scaleType = ScaleType::Linear;
void createScaleItem();
};
Plot.cpp
Code:
#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();
}
In my dialog I am using the plot and define to buttons to switch between linear and logarithmic scale.
.h
Code:
#include "ui_QwtPlotClient.h"
class Plot;
class QwtPlotClient
: public QDialog{
Q_OBJECT
public:
QwtPlotClient
(QWidget* parent
= nullptr
);
private:
Ui::QwtPlotClientClass m_ui;
Plot* m_plot = nullptr;
};
.cpp
Code:
#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);
}
When I start the program, the ticks of the QwtPlotScaleItem are ok - they match perfectly with the grid.
Attachment 13837
When I switch to the log scale, they are still perfectly matched to the grid.
Attachment 13838
But when I now switch back to the linear scale, the (minor) ticks are off the grid.
Attachment 13839
When I resize the plot, everything looks good again. What am I missing or is this a bug?
Important to know: we are using Qt 5.14.0 and I tested this code with Qwt 6.2.0, 6.2.1 and 6.3.0.
Re: Problems with QwtPlotScaleItem and switching scale engine
Bug has been fixed in all branches >= Qwt 6.3