I'm experiencing really odd behaviour with a Qwt plot. I'm plotting several datasets as QwtPlotCurves onto a QwtPlot, one dataset at a time, when a user clicks a button on the main app window.
First call to plotting object (creates QwtPlot) - first plot appears fine.
Second call to plotting object - no change; second plot doesn't appear.
Third call - both second and third plots now appear.
Fourth call - no change.
Fifth call - fourth and fifth plots appear.
etc
Below is the code from the plotting class.
DataPlotter
::DataPlotter(QWidget *parent,
QString strTitle,
double minLambda,
double maxLambda
): QwtPlot(parent
){
canvas()->setLineWidth(1);
setTitle(strTitle);
setAxisTitle
(QwtPlot::xBottom,
"Wavelength (nm)");
setAxisScale
(QwtPlot::xBottom, minLambda, maxLambda
);
setAxisTitle
(QwtPlot::yLeft,
"Normalized Power");
setAxisScale
(QwtPlot::yLeft,
0,
1.2);
}
void DataPlotter
::addPlot(double** data,
int numPoints,
QString strCurveLabel
) {
double x[numPoints];
double y[numPoints];
for (int i = 0; i < numPoints; i++){
x[i] = data[i][0];
y[i] = data[i][1];
}
c
->setPen
(QPen(Qt
::red));
c->attach(this);
c->setData(x, y, numPoints);
}
DataPlotter::DataPlotter(QWidget *parent, QString strTitle, double minLambda, double maxLambda): QwtPlot(parent)
{
canvas()->setFrameStyle(QFrame::Box | QFrame::Plain );
canvas()->setLineWidth(1);
insertLegend(new QwtLegend(), QwtPlot::BottomLegend);
setTitle(strTitle);
setAxisTitle(QwtPlot::xBottom, "Wavelength (nm)");
setAxisScale(QwtPlot::xBottom, minLambda, maxLambda);
setAxisTitle(QwtPlot::yLeft, "Normalized Power");
setAxisScale(QwtPlot::yLeft, 0, 1.2);
}
void DataPlotter::addPlot(double** data, int numPoints, QString strCurveLabel)
{
double x[numPoints];
double y[numPoints];
for (int i = 0; i < numPoints; i++){
x[i] = data[i][0];
y[i] = data[i][1];
}
QwtPlotCurve *c = new QwtPlotCurve(strCurveLabel);
c->setPen(QPen(Qt::red));
c->attach(this);
c->setData(x, y, numPoints);
}
To copy to clipboard, switch view to plain text mode
And here's the code calling the DataPlotter object from the main window, to update the plot:
void MainWindow
::drawCurvesOnPlotViewer(double** source,
int numLambda,
QString strTitle
) {
double minLambda = ui->selectMinLambda->text().toDouble();
double maxLambda = ui->selectMaxLambda->text().toDouble();
QString strPlotTitle
= "Transmittance Plots";
// check if plotViewer QWidget already exists
if (plotViewer == 0) {
plotViewer->setFixedSize(500,500);
plotViewer->setAttribute(Qt::WA_DeleteOnClose);
connect(plotViewer, SIGNAL(destroyed()), this, SLOT(closePlot()));
}
// check if DataPlotter object already exists
if (blnPlot == 0) {
plot = new DataPlotter(plotViewer,strPlotTitle,minLambda,maxLambda);
plot->addPlot(source,numLambda,strTitle);
layout->addWidget(plot);
plotViewer->setLayout(layout);
blnPlot = 1;
} else {
plot->addPlot(source,numLambda,strTitle);
}
plotViewer->show();
}
void MainWindow::drawCurvesOnPlotViewer(double** source, int numLambda, QString strTitle)
{
double minLambda = ui->selectMinLambda->text().toDouble();
double maxLambda = ui->selectMaxLambda->text().toDouble();
QString strPlotTitle = "Transmittance Plots";
QLayout *layout;
// check if plotViewer QWidget already exists
if (plotViewer == 0) {
plotViewer = new QWidget();
plotViewer->setFixedSize(500,500);
plotViewer->setAttribute(Qt::WA_DeleteOnClose);
connect(plotViewer, SIGNAL(destroyed()), this, SLOT(closePlot()));
}
// check if DataPlotter object already exists
if (blnPlot == 0) {
plot = new DataPlotter(plotViewer,strPlotTitle,minLambda,maxLambda);
plot->addPlot(source,numLambda,strTitle);
layout = new QHBoxLayout;
layout->addWidget(plot);
plotViewer->setLayout(layout);
blnPlot = 1;
} else {
plot->addPlot(source,numLambda,strTitle);
}
plotViewer->show();
}
To copy to clipboard, switch view to plain text mode
Any ideas as to what could be causing this? Any suggestions would be greatly appreciated, as I've been banging my head over this all day.
Cheers!
Bookmarks