Hi all, first post in the forum. I had the same resize problem as the OP.
I wanted to show a bar plot:
BarChart
::BarChart(QWidget *parent
) : QwtCustomPlot(parent)
{
QwtColumnSymbol *columnSymbol;
columnSymbol = new QwtColumnSymbol(QwtColumnSymbol::Box);
columnSymbol->setLineWidth(0);
columnSymbol->setFrameStyle(QwtColumnSymbol::NoFrame);
columnSymbol
->setPalette
(QPalette("Red"));
_barChart = new QwtPlotBarChart();
_barChart->setLayoutPolicy(QwtPlotBarChart::AutoAdjustSamples);
_barChart->setSpacing(2);
_barChart->setMargin(0);
_barChart->setSymbol(columnSymbol);
_barChart->attach(this);
}
BarChart::BarChart(QWidget *parent) :
QwtCustomPlot(parent)
{
QwtColumnSymbol *columnSymbol;
columnSymbol = new QwtColumnSymbol(QwtColumnSymbol::Box);
columnSymbol->setLineWidth(0);
columnSymbol->setFrameStyle(QwtColumnSymbol::NoFrame);
columnSymbol->setPalette(QPalette("Red"));
_barChart = new QwtPlotBarChart();
_barChart->setLayoutPolicy(QwtPlotBarChart::AutoAdjustSamples);
_barChart->setSpacing(2);
_barChart->setMargin(0);
_barChart->setSymbol(columnSymbol);
_barChart->attach(this);
setAxisAutoScale(QwtPlot::yLeft);
}
To copy to clipboard, switch view to plain text mode
inherited from the custom plotter
QwtCustomPlot
::QwtCustomPlot(QWidget *parent
) :{
((QFrame *)canvas
())->setLineWidth
(0);
plotLayout()->setCanvasMargin(0);
setAttribute(Qt::WA_Hover);
setAutoFillBackground(true);
enableAxis
(QwtPlot::xBottom,
false);
setAxisTitle
(QwtPlot::xBottom, axisText
);
setAxisTitle
(QwtPlot::yLeft, axisText
);
}
QwtCustomPlot::QwtCustomPlot(QWidget *parent) :
QwtPlot(parent)
{
((QFrame *)canvas())->setLineWidth(0);
((QFrame *)canvas())->setFrameStyle(QFrame::NoFrame);
((QFrame *)canvas())->setPalette(QColor("White"));
plotLayout()->setCanvasMargin(0);
setAttribute(Qt::WA_Hover);
setAutoFillBackground(true);
enableAxis(QwtPlot::xBottom, false);
enableAxis(QwtPlot::yLeft, false);
QwtText axisText;
axisText.setFont(QApplication::font());
setAxisTitle(QwtPlot::xBottom, axisText);
setAxisTitle(QwtPlot::yLeft, axisText);
setAxisFont(QwtPlot::xBottom, QApplication::font());
setAxisFont(QwtPlot::yLeft, QApplication::font());
}
To copy to clipboard, switch view to plain text mode
which sets the canvas margin to 0, hence filling the canvas with the bar plot.
Since a bar is 1 unit wide, the x axis range of n bars goes from -0.5 to n-0.5. I scale it manually at each update:
void BarChart::setBars(const QVector<double> &values)
{
if(values.isEmpty())
return;
setAxisScale(
-0.5,
values.size()-0.5);
_barChart->setSamples(values);
}
void BarChart::setBars(const QVector<double> &values)
{
if(values.isEmpty())
return;
setAxisScale(
QwtPlot::xBottom,
-0.5,
values.size()-0.5);
_barChart->setSamples(values);
}
To copy to clipboard, switch view to plain text mode
This works as long as I don't resize the plot, with the bar at the extremes adjacent to the widget border.
However, resizing the plot resets the canvas margins to 0.5 units to the extremes, hence changing the layout.
The solution was to force the canvas margins to follow the scale
plotLayout()->setAlignCanvasToScales(true);
plotLayout()->setAlignCanvasToScales(true);
To copy to clipboard, switch view to plain text mode
The resize now keeps the layout unaltered!
Bookmarks