Question

I have a thread that reads data from an external device once per second. I want to draw a graph that displays this data using qwt but I'm having trouble getting it to draw anything.

I've added a slot to my dialog which is passed the data to be drawn and a label on the UI for it to draw to, but all I'm getting right now is a dot appearing in the top left hand corner. The slot code looks like this:

void Dialog::updateGraph(double *pdXData, double *pdYData, unsigned int nCount)
{
    QwtPlotCurve curve;
    curve.setPen( Qt::darkBlue );
    curve.setStyle( QwtPlotCurve::Lines );
    curve.setRenderHint( QwtPlotItem::RenderAntialiased );

    curve.setRawSamples(pdXData, pdYData, nCount);

    QwtScaleMap xMap;
    QwtScaleMap yMap;

    xMap.setScaleInterval(0, nCount - 1);
    yMap.setScaleInterval(0, 50); // TODO - max

    QPixmap pixmap(300, 200);
    QPainter    painter(&pixmap);
    curve.draw(&painter, xMap, yMap, ui->graph->rect());
    ui->graph->setPixmap(pixmap);
    ui->graph->show();
}

UPDATE: If I create pixmap with a picture then that is drawn, but still no graph.

Was it helpful?

Solution

I fixed my problem with using Qwt in Qt Creator (see Can't get qwt_designer_plugin to load in Qt Creator) so I was able to change the bitmap to a QwtPlot. I then found I still didn't get a graph until I made the QwtPlotCurve a member of the class; I think it stores the data that gets drawn to the graph so needs to exist during the repaint. Finally, I fixed a memory leak and found that my array of doubles wasn't being cloned by Qt so I changed it to QVector.

The working code looks like this:

void Dialog::updateGraph(QVector<QPointF> data)
{
    _curve.setPen( Qt::darkBlue );
    _curve.setStyle( QwtPlotCurve::Lines );
    _curve.setRenderHint( QwtPlotItem::RenderAntialiased );

    _curve.setSamples(data);

    ui->qwtPlot->setAxisScale(QwtPlot::xBottom, 0.0, data.size() - 1, 1);
    ui->qwtPlot->setAxisScale(QwtPlot::yLeft, 0.0, 50.0, 10.0); // TODO - max

    _curve.attach(ui->qwtPlot);
    ui->qwtPlot->replot();
}

OTHER TIPS

You can only update the graphique from the main thread

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top