Qt & qwtPlot --> keine plots bei Aufruf der Fkt. ausserhalb des plot Moduls

Oxid2178

Member
Hallo Leute

Ich versuche vorerst einige Testkurven in Qt4 mit der Library qwtplot anzuzeigen. Beim Aufruf der Funktion populate() im Konstruktor Plot::Plot..., werden die Testkurven angezeigt.
Wenn ich aber die Fkt. populate() im module modbusadapter.cpp (nach einer modbus response) aufrufe, dann wird leider nichts geplottet. Im Debug Mode kann ich sehen, dass der Spung in populate() stattfindet, wird aber eben nichts angezeigt.

Ich verwende zwei separate Fenster, einer für die Modbus Kommunikation und das andere Fenster für die Plots. Ich hoffe nicht, dass diese zwei "Instanzen" das Problem ist.

Hier einige Code Ausschnitte:

plot.h
Code:
#ifndef PLOT_H
#define PLOT_H

#include <qapplication.h>
#include <qlayout.h>
#include <qwt_plot.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_marker.h>
#include <qwt_plot_curve.h>
#include <qwt_legend.h>
#include <qwt_series_data.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_magnifier.h>
#include <qwt_text.h>
#include <qwt_math.h>
#include <math.h>

#define SIZE 15

class Plot : public QwtPlot
{
    Q_OBJECT

public:
    Plot(QWidget * = NULL);
    virtual ~Plot();
    void setPlotValue(double value);
    void populate();

protected:
    virtual void resizeEvent( QResizeEvent * );

private:
    void updateGradient();
    double cntX;
    QwtPlotCurve *density;
    double xData[SIZE];
    double yData[SIZE];

public slots:
    void addNewValue(double value);

signals:

};
#endif // PLOT_H


plot.cpp
Code:
#include "plot.h"

Plot::Plot(QWidget *parent):
    QwtPlot( parent )
{
    xData[0] = 0;
    yData[0] = 0;

    // Insert new density curve
    density = new QwtPlotCurve("y = density");
    //density->setRenderHint(QwtPlotItem::RenderAntialiased);
    density->setStyle(QwtPlotCurve::Lines);
    density->setLegendAttribute(QwtPlotCurve::LegendShowLine, true);
    density->setPen(QPen(Qt::red));
    density->attach(this);

    // Draw grid
    QwtPlotGrid *grid = new QwtPlotGrid();
    grid->setPen(QPen(Qt::gray, 0.0, Qt::DotLine));
    grid->enableX(true);
    grid->enableXMin(true);
    grid->enableY(true);
    grid->enableYMin(false);
    grid->attach(this);

    // Insert markers
    //  ...a horizontal line at y = 0...
    QwtPlotMarker *mY = new QwtPlotMarker();
    mY->setLabel(QString::fromLatin1("y = 0"));
    mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
    mY->setLineStyle(QwtPlotMarker::HLine);
    mY->setYValue(0.0);
    mY->attach(this);

    //  ...a vertical line at x = 0...
    QwtPlotMarker *mX = new QwtPlotMarker();
    mX->setLabel(QString::fromLatin1("x = 0"));
    mX->setLabelAlignment(Qt::AlignLeft | Qt::AlignBottom);
    mX->setLabelOrientation(Qt::Vertical);
    mX->setLineStyle(QwtPlotMarker::VLine);
    mX->setLinePen(QPen(Qt::black, 0, Qt::DashDotLine));
    mX->setXValue(0.0);
    mX->attach(this);

    // panning with the left mouse button
    (void) new QwtPlotPanner( canvas() );

    // zoom in/out with the wheel
    (void) new QwtPlotMagnifier( canvas() );

    setAutoFillBackground( true );
    setPalette( QPalette( QColor( 165, 193, 228 ) ) );
    updateGradient();

    setTitle("A Simple QwtPlot Demonstration");
    insertLegend(new QwtLegend(), QwtPlot::RightLegend);

    // axes
    setAxisTitle(xBottom, "x -->" );
    setAxisScale(xBottom, 0.0, 10.0);

    setAxisTitle(yLeft, "y -->");
    setAxisScale(yLeft, -1.0, 1.0);

    // canvas
    canvas()->setLineWidth( 1 );
    canvas()->setFrameStyle( QFrame::Box | QFrame::Plain );
    canvas()->setBorderRadius( 15 );

    QPalette canvasPalette( Qt::white );
    canvasPalette.setColor( QPalette::Foreground, QColor( 133, 190, 232 ) );
    canvas()->setPalette( canvasPalette );

    //populate();         // beim Aufruf von hier aus, funktionieren die Plots...
}

Plot::~Plot()
{

}

void Plot::populate()
{
    qWarning()<<  "Plot : populate()";

    double maxD = 0;
    double minD = 1;
    double maxS = 0;
    double minS = 1;

    double dens = 2.0;
    unsigned int sample = 0;
    unsigned int x = 0;
    unsigned int y = 0;

    while(sample < 5)
    {
        xData[x++]= sample;
        yData[y++]= dens;

        maxS = sample;
        minS = sample++;
        maxD = dens;
        minD = dens++;

        setAxisScale(xBottom, (int)minS-1, (int)maxS+1);
        setAxisScale(yLeft,   (int)minD-1, (int)maxD+1);

        density->setSamples(xData, yData, sample);
    }
    setAutoReplot(true);
    // Call "replot()" to refresh plot displaying
    replot();
}
 
Zuletzt bearbeitet:
hier noch den Rest:

main
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "plot.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Plot *plot = new Plot();

    // We put a dummy widget around to have
    // so that Qt paints a widget background
    // when resizing

    QWidget window;
    QHBoxLayout *layout = new QHBoxLayout( &window );
    layout->setContentsMargins( 0, 0, 0, 0 );
    layout->addWidget( plot );

    window.resize(600,400);
    window.show();

    MainWindow w;
    w.show();

    return a.exec();
}


modbusadapter.h
Code:
#ifndef MODBUSADAPTER_H
#define MODBUSADAPTER_H

#include <QObject>
#include "modbus.h"
#include "registersmodel.h"
#include "rawdatamodel.h"
#include <QTimer>
#include "eutils.h"

#include "plot.h"

class ModbusAdapter : public QWidget
{
    Q_OBJECT

public:
    explicit ModbusAdapter(QWidget *parent = 0);

     void busMonitorRequestData(uint8_t * data,uint8_t dataLen);
     void busMonitorResponseData(uint8_t * data,uint8_t dataLen);

     void modbusConnectRTU(QString port, int baud, QChar parity, int dataBits, int stopBits);
     void modbusConnectTCP(QString ip, int port);
     void modbusDisConnect();
     void modbusRequestData(int slave, int functionCode, int startAddress, int noOfItems);
     RegistersModel *regModel;
     RawDataModel *rawModel;
     Plot *plotNewDensity;
     bool isConnected();

private:
     modbus_t * m_modbus;
     bool m_connected;
     bool m_RTUSelected;

signals:
     void newDensityValue(double value);

public slots:

};

#endif // MODBUSADAPTER_H


modbusadapter.cpp
Code:
#include "modbusadapter.h"
#include <QMessageBox>
#include <QtDebug>
#include <errno.h>

ModbusAdapter *m_instance;
static const int DBG = false; //Debug messages from libmodbus

ModbusAdapter::ModbusAdapter(QWidget *parent) :
    QWidget(parent),
    m_modbus(NULL)
{
    m_instance=this;
    regModel=new RegistersModel(this);
    rawModel=new RawDataModel(this);

    plotNewDensity=new Plot(this);

    m_connected = false;
}

...
...
...
            case _FC_READ_INPUT_REGISTERS:
                    ret = modbus_read_input_registers(m_modbus, startAddress, noOfItems, dest16);
                    is16Bit = true;
                    break;
...
...
...
            // nach modbus response...
            plotNewDensity->populate();
...
...
...
            }


Bin froh um jeden Tipp.

Danke und Gruss
Oxid
 
noch einige Bilder...

plotOK.gif --> OK Aufruf von populate() im Konstruktor PLot::Plot...:

plotNOK.gif--> NOK Aufruf von populate() im Modul modbusadapter.cpp:

window_qwtPlot&modbus.gif --> Die zwei separaten Fenster der Anwendung:
 

Anhänge

  • plotOk.gif
    plotOk.gif
    40 KB · Aufrufe: 604
  • plotNok.gif
    plotNok.gif
    39,3 KB · Aufrufe: 616
  • window_qwtPlot&modbus.gif
    window_qwtPlot&modbus.gif
    150,6 KB · Aufrufe: 656
Zurück
Oben