Hello Qt gurus,
I have written a code that runs a step by step analysis and in after each step, updates a plot of the results. For the plot, I have sub-classed QwtPlot as follows:
RPlotWidget
::RPlotWidget(QWidget *parent
){
setCanvasBackground
(QColor(Qt
::white));
insertLegend
(legend,
QwtPlot::TopLegend);
setMargin(5);
setCanvasLineWidth(0);
plotLayout()->setCanvasMargin(0);
plotLayout()->setSpacing(0);
QFont labelFont
("Times New Roman",
10);
setAxisFont
(QwtPlot::xBottom, labelFont
);
setAxisFont
(QwtPlot::yLeft, labelFont
);
QFont titleFont
("Times New Roman",
12,
QFont::Bold);
QwtText bottomText
= bottomAxis
->title
();
bottomText.setFont(titleFont);
bottomAxis->setTitle(bottomText);
bottomAxis->setMargin(0);
QwtText leftText
= leftAxis
->title
();
leftText.setFont(titleFont);
leftAxis->setTitle(leftText);
leftAxis->setMargin(0);
plotMagnifier->setEnabled(true);
plotMagnifier->setMouseButton(Qt::MidButton);
plotPanner->setEnabled(true);
plotPanner->setMouseButton(Qt::LeftButton);
plotZoomer->setMousePattern(0, Qt::LeftButton, Qt::ControlModifier);
plotZoomer->setMousePattern(1, Qt::NoButton);
plotZoomer->setMousePattern(2, Qt::NoButton);
plotZoomer->setEnabled(true);
}
RPlotWidget::RPlotWidget(QWidget *parent)
: QwtPlot(parent)
{
setCanvasBackground(QColor(Qt::white));
QwtLegend *legend = new QwtLegend(this);
insertLegend(legend, QwtPlot::TopLegend);
setMargin(5);
setCanvasLineWidth(0);
plotLayout()->setCanvasMargin(0);
plotLayout()->setSpacing(0);
QFont labelFont("Times New Roman", 10);
setAxisFont(QwtPlot::xBottom, labelFont);
setAxisFont(QwtPlot::yLeft, labelFont);
QFont titleFont("Times New Roman", 12, QFont::Bold);
QwtScaleWidget *bottomAxis = axisWidget(QwtPlot::xBottom);
QwtText bottomText = bottomAxis->title();
bottomText.setFont(titleFont);
bottomAxis->setTitle(bottomText);
bottomAxis->setMargin(0);
QwtScaleWidget *leftAxis = axisWidget(QwtPlot::yLeft);
QwtText leftText = leftAxis->title();
leftText.setFont(titleFont);
leftAxis->setTitle(leftText);
leftAxis->setMargin(0);
QwtPlotMagnifier *plotMagnifier = new QwtPlotMagnifier(canvas());
plotMagnifier->setEnabled(true);
plotMagnifier->setMouseButton(Qt::MidButton);
QwtPlotPanner *plotPanner = new QwtPlotPanner(canvas());
plotPanner->setEnabled(true);
plotPanner->setMouseButton(Qt::LeftButton);
QwtPlotZoomer *plotZoomer = new QwtPlotZoomer(canvas(), true);
plotZoomer->setMousePattern(0, Qt::LeftButton, Qt::ControlModifier);
plotZoomer->setMousePattern(1, Qt::NoButton);
plotZoomer->setMousePattern(2, Qt::NoButton);
plotZoomer->setEnabled(true);
}
To copy to clipboard, switch view to plain text mode
I am using the above class in my analysis as follows:
// Declare the plotting variables
QPointer<QwtPlot> thePlot = 0;
QVector<double> xVector;
xVector.append(0.0);
QVector<double> y1Vector;
y1Vector.append(0.0);
QVector<double> y2Vector;
y2Vector.append(0.0);
int x = 0;
while (x <= maxIterations) //Stop condition of the analysis
{
x++;
// Instatiating thePlot
if (!thePlot) {
thePlot = new RPlotWidget(mainWindow);
thePlot->show();
thePlot
->setAxisTitle
(QwtPlot::xBottom,
"x");
thePlot
->setAxisTitle
(QwtPlot::yLeft,
"y");
// Instantiating two curves
y1Curve
->setPen
(QColor(Qt
::red));
y1Curve
->setRenderHint
(QwtPlotItem::RenderAntialiased,
true);
y1Curve->attach(thePlot);
y2Curve
->setPen
(QColor(Qt
::blue));
y2Curve
->setRenderHint
(QwtPlotItem::RenderAntialiased,
true);
y2Curve->attach(thePlot);
}
double y1, y2;
// ... Run the analysis, evaluate y1 and y2
// Updating the plotting value vectors
xVector.append(x);
y1Vector.append(y1);
y2Vector.append(y2);
// Updating the plot
y1Curve->setData(xVector, y1Vector);
y2Curve->setData(xVector, y2Vector);
thePlot->replot();
}
// Declare the plotting variables
QPointer<QwtPlot> thePlot = 0;
QwtPlotCurve *y1Curve = 0;
QwtPlotCurve *y2Curve = 0;
QVector<double> xVector;
xVector.append(0.0);
QVector<double> y1Vector;
y1Vector.append(0.0);
QVector<double> y2Vector;
y2Vector.append(0.0);
int x = 0;
while (x <= maxIterations) //Stop condition of the analysis
{
x++;
// Instatiating thePlot
if (!thePlot) {
thePlot = new RPlotWidget(mainWindow);
thePlot->show();
thePlot->setAxisTitle(QwtPlot::xBottom, "x");
thePlot->setAxisTitle(QwtPlot::yLeft, "y");
// Instantiating two curves
y1Curve = new QwtPlotCurve(QString("y1"));
y1Curve->setPen(QColor(Qt::red));
y1Curve->setRenderHint(QwtPlotItem::RenderAntialiased, true);
y1Curve->attach(thePlot);
y2Curve = new QwtPlotCurve(QString("y2"));
y2Curve->setPen(QColor(Qt::blue));
y2Curve->setRenderHint(QwtPlotItem::RenderAntialiased, true);
y2Curve->attach(thePlot);
}
double y1, y2;
// ... Run the analysis, evaluate y1 and y2
// Updating the plotting value vectors
xVector.append(x);
y1Vector.append(y1);
y2Vector.append(y2);
// Updating the plot
y1Curve->setData(xVector, y1Vector);
y2Curve->setData(xVector, y2Vector);
thePlot->replot();
}
To copy to clipboard, switch view to plain text mode
This code works perfectly in Windows, but it crashes while running the "setData(...)" for the second iteration in Mac OS X. I have gone through all the threads about Qwt and Mac, yet I have not found any solutions .
I am using Qt 4.5.2 and Qwt 5.2.0. The Qwt is built statically.
Thanks a lot for any help in advance.
Mojtaba
Bookmarks