Seite 1 von 1

Keine QwtPlotCurves sichtbar

Verfasst: 8. Januar 2010 14:19
von Vitali
Hallo,

ich habe ein Problem beim Ploten von Funktionen mit Hilfe von QwtPlot und QwtPlotCurve. Und zwar erstelle ich zunaechst eine Liste mit QwtPlots und spaeter haenge ich an diese die neu erstellten QwtPlotCurves an. Davor werde die Daten an die QwtPlotCurves uebergeben (siehe Code unten).

Komischerweise kommt es erst gar nicht zur Berechnung der x und y Werte in der Klasse "CurveData" (es gibt keinen Output in der Konsole). Also nehme ich an, dass deswegen nix in den Plots geplotet wird, aber wo ist der Fehler in meinem Code? Helfen Sie mir bitte.

Hier ist die Funktion, in der ich neue Kurven erstelle und Daten dafuer generiere:

Code: Alles auswählen

void OscillationGRN::plotCurves(){

	for(int j = 0; j < ListOfPlots.size(); ++j){
			//create new Curve
		QwtPlotCurve *newCurve = new QwtPlotCurve();
		newCurve->setPen(QColor(Qt::red));
		newCurve->setStyle(QwtPlotCurve::Lines);
		newCurve->setCurveAttribute(QwtPlotCurve::Fitted);

		double range[] ={0,20};
		double step = 0.1;
		// copy the data into the curves
			newCurve->setData(CurveData(range, step));
		newCurve->attach(ListOfPlots[j]);

	}
}
Dabei ist ListOfPlots ein Vektor mit den QwtPlots:

Code: Alles auswählen

QVector<QwtPlot*> ListOfPlots;
Hier is die Klasse, die mir x und y Werte liefern soll:

Code: Alles auswählen

class CurveData: public QwtData
{
    // The x values depend on its index and the y values
    // can be calculated from the corresponding x value.
    // So we don't need to store the values.
    // Such an implementation is slower because every point
    // has to be recalculated for every replot, but it demonstrates how
    // QwtData can be used.

public:
	CurveData( double rang1[2], double step)
    {
    	rangeX[0]= rang1[0];	rangeX[1] = rang1[1];
    	range = (rangeX[1] - rangeX[0])/step;
    	if(range<0){
    		range = range *(-1);
    	}
    	d_size = size_t(range);
    	cout << "range = "<< range << " , d_size= " << d_size<< endl;
    }

    virtual QwtData *copy() const
    {
        return new CurveData(*this);
    }

    virtual size_t size() const
    {
        return d_size;
    }

    virtual double x(size_t i) const
    {
    	cout << "x = " << rangeX[0] + i*((rangeX[1]- rangeX[0])/range) << endl;
    	// here the array with x values will be produced
    	return rangeX[0] + i*((rangeX[1]- rangeX[0])/range);
    }

    virtual double y(size_t i) const
    {
    	cout << "y = " << sin(2*3.1415926*x(i)/T)+1.0 << endl;
    	return sin(2*3.1415926*x(i)/T)+1.0;
    }
private:
    size_t d_size;
    double range;
	double rangeX[2];

};

Verfasst: 8. Januar 2010 14:51
von Vitali
Ich habe gerade auch versucht ganz einfach die x und y Werte selbst zu erstellen und dann der Kurve anfuegen, aber das klappt auch nicht (nichts auf dem Plot zu sehen).

Code: Alles auswählen

void OscillationGRN::plotCurves(){

	for(int j = 0; j < ListOfPlots.size(); ++j){
			//create new Curve
		QwtPlotCurve *newCurve = new QwtPlotCurve();
		newCurve->setPen(QColor(Qt::red));
		newCurve->setStyle(QwtPlotCurve::Lines);
		newCurve->setCurveAttribute(QwtPlotCurve::Fitted);
		const unsigned Size=200;
		double xval[Size];
		double yval[Size];
		double ti=0;
			for(unsigned i=0; i<Size; ++i){
				xval[i] = ti;
			  yval[i] = sin(2*3.1415926*ti/T)+1.0;
			  ti= ti+0.1;
			}

			newCurve->setRawData(xval,yval, Size);
		newCurve->attach(ListOfPlots[j]);

	}
}

Verfasst: 8. Januar 2010 15:07
von Vitali
Sorry fuer die unsinnigen posts, denn ich habe ja das wichtigste beim Plot vergessen und zwar das reploten!

Code: Alles auswählen

ListOfPlots[j]->replot();