Probleme beim Plotten von Geraden mit QWT
-
Hallo,
ich bin neu in der Welt der GUI und habe Probleme beim Schreiben eines Programms zum Plotten von Geraden.
Wäre sehr nett, wenn mir da einer weiterhelfen könnte...
Ich programmiere mit Visual Studio 2010 Express.
Sowohl QT als auch QWT hab ich erfolgreich eingebunden und auch dahingehend getestet
Vorgestellt hatte ich mir das wie folgt:- Im Hauptfenster (Plotm.h/cpp) werden die Parameter(Steigung + Offset) der Geraden eingegeben
- Drückt man nun den Plot-Button sollte zunächst ein Konstruktor aufgerufen werden,
der mir einen QWT Plot namens Graph erstellt und dort eine Gerade mit den übergebenen Parametern einfügt
(Plot.h/cpp)- Desweitern soll ein QDialog erstellt werden
- In den erstellten QDialog wird der zuvor erstellt Graph eingefügt
- Nun soll sich das Fenster mitsamt dem Graphen öffnen
Mein Problem ist nun, dass ich nach erfolgreichem Kompilieren und Linken zwar den Dialog aufrufen kann und dieser mir auch angezeigt wird,
sich aber sobald ich den Konstruktor für den Graphen aufrufe das Fenster schließt
und sich das Programm mit Fehlercode 1 komplett beendet.Plotm.h:
#ifndef PLOTM_H #define PLOTM_H #include "ui_Plot10.h" #include <qdialog.h> class Plotm : public QMainWindow, public Ui::MainWindow { Q_OBJECT public: Plotm (QMainWindow *parent = 0); ~ Plotm (); private slots: void draw(); }; #endif //PLOTM_H
Plotm.cpp:
#include "Plotm.h" #include "Plot.h" Plotm::Plotm(QMainWindow *parent) : QMainWindow(parent) { setupUi(this); xmi-> setText("0"); ymi-> setText("0"); connect(plotb, SIGNAL(clicked()), this, SLOT(draw())); } Plotm::~Plotm() {} void Plotm::draw() { Graph* plot ; QDialog* nw; nw = new QDialog(this, Qt::Window); int ava; //Steigung int bva; //Offset ava = aval->text().toDouble(); bva = bval->text().toDouble(); plot = new Graph(ava,bva,nw); plot->show(); nw->show(); }
Plot.h:
#ifndef PLOT_H #define PLOT_H #include <qapplication.h> #include <qlayout.h> #include <qwt_plot.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> #include <qwt_symbol.h> #include <QtGui> class Graph : public QwtPlot { public: int aval;//Steigung int bval;//Offset double xval[100]; double yval[100]; //Konstruktor der die Parameter für die Gerade übergibt Graph(int avala,int bvala, QWidget* parent); }; #endif
Plot.cpp:
#include <qapplication.h> #include <qlayout.h> #include <qwt_plot.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> #include <qwt_symbol.h> #include "Plot.h" #include <qwt_plot_grid.h> #include "Plotm.h" #include "Plot.h" Graph::Graph(int avala,int bvala,QWidget *parent) { aval = avala; bval = bvala; setAutoFillBackground( true ); setPalette( QPalette( QColor( 165, 193, 228 ) ) ); setTitle("Plot 1.0"); insertLegend(new QwtLegend(), QwtPlot::RightLegend); // axes setAxisTitle(xBottom, "x -->" ); setAxisScale(xBottom, 0, 10); setAxisTitle(yLeft, "y -->"); setAxisScale(yLeft, 0, 10); // 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 ); for(int i=0; i<100;i++) { xval[i] = double(i) * 1/10; yval[i] = this->aval * xval[i] + this->bval; } // Insert new curves QwtPlotCurve * d_curves = new QwtPlotCurve("y = a*x+b"); d_curves->setRenderHint(QwtPlotItem::RenderAntialiased); d_curves->setLegendAttribute(QwtPlotCurve::LegendShowLine, true); d_curves->setPen(QPen(Qt::green)); d_curves->setRawSamples(xval, yval, 100); d_curves->attach(this); replot(); }
-
Haste mal geschaut, wo genau es kracht und was für eine Art von Fehler vorliegt?
-
Also wenn ich im Sourcecode in der Funktion Draw in Plotm.cpp den Konstruktor von Graph weglasse läuft das ganze einwandfrei...bekomm dann halt logischerweise en leeres neues Fenster...der Fehler liegt also meiner Laien-Meinung nach wohl irgendwo beim Aufruf des Konstruktors von Graph.
Und wenn ich halt Draw inklusive Graph-Konsgtruktor per Klick auf den Button aufrufe schließt sich das komplette Programm und Visual Studio meint in seinem Protokoll, dass das Programm mit Fehlercoder 1 beendet wurde.
Hoffe das es das war, was du wissen wolltest...
-
Kannst du mal mit dem Debugger schritt für schritt durch den Konstruktor durchgehen und gucken, wo genau es crasht?
-
sollte der ctor nicht auch den ctor seiner basisklasse aufrufen ? oder is
das bei qt nicht immer notwendig ?
-
Also ich habe jetzt noch den Konstruktor der Basisklasse aufgerufen und noch an anderer Stelle versucht den Code zu korrigieren...leider bleibt das Problem das selbe.
@Q:
Könntest du mir genauer erklären, wie es möglich is diese Informationen vom Debugger zu bekommen?
Denn der Build verläuft ja erfolgreich und der Fehler tritt erst beim Betätigen des Buttons auf...Hier mal noch der neue Code:
Plot.h
class Graph : public QwtPlot { public: Graph(QWidget* parent); QwtPlot *myPlot; }; #endif
Plot.cpp
Graph::Graph(QWidget *parent) : QwtPlot (parent) { myPlot= new QwtPlot(parent); myPlot->setAutoFillBackground( true ); myPlot->setPalette( QPalette( QColor( 165, 193, 228 ) ) ); myPlot->setTitle("Plot 1.0"); myPlot->insertLegend(new QwtLegend(), QwtPlot::RightLegend); // axes myPlot->setAxisTitle(xBottom, "x -->" ); myPlot->setAxisScale(xBottom, 0, 10); myPlot->setAxisTitle(yLeft, "y -->"); myPlot->setAxisScale(yLeft, 0, 10); myPlot->setAutoReplot(true); }
Plotm.h
#define PLOTM_H #include "ui_Plot10.h" #include <qdialog.h> class Plotm : public QMainWindow, public Ui::MainWindow { Q_OBJECT public: Plotm (QMainWindow *parent = 0); ~ Plotm (); int avalue; int bvalue; int xmax; int xmin; int ymax; int ymin; double xval[100]; double yval[100]; private slots: void draw(); }; #endif //PLOTM_H
Plotm.cpp
#include "Plotm.h" #include "Plot.h" Plotm::Plotm(QMainWindow *parent) : QMainWindow(parent) { setupUi(this); xmi-> setText("0"); ymi-> setText("0"); connect(plotb, SIGNAL(clicked()), this, SLOT(draw())); } Plotm::~Plotm() {} void Plotm::draw() { Graph* plot ; QDialog* nw; QHBoxLayout *layout; nw = new QDialog(this, Qt::Window); plot = new Graph(nw); layout = new QHBoxLayout(nw); layout->setContentsMargins( 0, 0, 0, 0 ); layout->addWidget(plot); int ava; int bva; int xmax; int xmin; int ymax; int ymin; ava = aval->text().toDouble(); bva = bval->text().toDouble(); xmax = xma->text().toDouble(); xmin = xmi->text().toDouble(); ymax = yma->text().toDouble(); ymin = ymi->text().toDouble(); double xval[100]; double yval[100]; for(int i=0; i<100;i++) { xval[i] = double(i) * ((xmax - xmin) / 100); yval[i] = ava * xval[i] + bva; } QwtPlotCurve * d_curves = new QwtPlotCurve("y = a*x+b"); d_curves->setRenderHint(QwtPlotItem::RenderAntialiased); d_curves->setLegendAttribute(QwtPlotCurve::LegendShowLine, true); d_curves->setPen(QPen(Qt::green)); d_curves->setRawSamples(xval, yval, 100); d_curves->attach(plot); nw->resize(800,600); plot->show(); nw->show(); }