Probleme mit Qwt
-
Hallo,
ich hab angefangen Qwt zu verwenden und habe einige Probleme damit.
Wenn ich ein Diagramm erstelle klappt es schon.#include <src/qwt_plot_curve.h> #include <src/qwt_plot.h> #include <qapplication.h> #include <qcolor.h> int main(int argc, char **argv) { QApplication a(argc, argv); double x[101]; double y[101]; for ( int i = 0; i < 101; i++ ) { x[i] = i /10; y[i] = x[i]; } QwtPlot plot; QwtPlotCurve *curve = new QwtPlotCurve(); curve->setPen(QColor(Qt::darkBlue)); curve->setStyle(QwtPlotCurve::Lines); curve->setRenderHint(QwtPlotItem::RenderAntialiased); curve->setRawSamples(x, y, 101); curve->attach(&plot); plot.show(); return a.exec();
Wenn ich es aber in ein anderes Programm einfüge funktioniert das Diagramm nicht.
main.cpp:
#include <QApplication> #include "test.h" int main(int argc, char *argv[]){ QApplication a(argc, argv); test t; t.show(); return a.exec(); }
test.cpp:
#include "test.h" #include <src/qwt_plot_curve.h> #include <src/qwt_plot.h> #include <qapplication.h> #include <qcolor.h> test::test (QWidget *parent) : QWidget(parent){ layout = new QGridLayout(this); wechsel = new QPushButton("Wechseln"); close = new QPushButton("Beenden"); l = new QLabel("Qwt - Test"); w = true; int n = 101; double x[n]; double y[n]; for ( int i = 0; i < n; i++ ) { x[i] = i / 10.0; y[i] = x[i]; } double x2[n]; double y2[n]; for ( int k = 0; k < n; k++ ) { x2[k] = k / 10.0; y2[k] = x2[k]; } curve = new QwtPlotCurve; curve->setPen(QColor(Qt::darkBlue)); curve->setStyle(QwtPlotCurve::Lines); curve->setRenderHint(QwtPlotItem::RenderAntialiased); curve->setRawSamples(x, y, n); curve->attach(&plot); curve2 = new QwtPlotCurve; /*curve2->setPen(QColor(Qt::darkBlue)); curve2->setStyle(QwtPlotCurve::Lines); curve2->setRenderHint(QwtPlotItem::RenderAntialiased);*/ curve2->setRawSamples(x2, y2, n); curve2->attach(&plot2); plot2.hide(); layout->setMargin(3); layout->setColumnMinimumWidth(0,400); layout->setRowMinimumHeight(1,400); layout->addWidget(l,0,0); layout->addWidget(&plot,1,0); layout->addWidget(&plot2,1,0); layout->addWidget(wechsel,2,0); layout->addWidget(close,3,0); connect(wechsel, SIGNAL(clicked()), this, SLOT(wechseln())); connect(close, SIGNAL(clicked()), this, SLOT(close())); } void test::wechseln(){ if(w == true){ plot.hide(); plot2.show(); w = false; }else{ plot.show(); plot2.hide(); w = true; } }
test.h:
#include <QGridLayout> #include <QPushButton> #include <QLabel> #include <src/qwt_plot_curve.h> #include <src/qwt_plot.h> class test : public QWidget{ Q_OBJECT public: test(QWidget *parent = 0); private slots: void wechseln(); public: QwtPlot plot, plot2; QwtPlotCurve *curve,*curve2; QGridLayout *layout; QPushButton *wechsel,*close; QLabel *l; bool w; };
Warum funktioniert das nicht?
MfG ch_1992
-
int n = 101; double x[n]; double y[n];
Das ist falsch! n ist keine Konstante, und nur mit Konstanten kannst du ohne new arrays anlegen. Mit "const int n" geht es.
Das eigentliche Problem: x und y sind lokale Variablen im Konstruktor, sie werden beim Verlassen des Konstruktors zerstört. Die Doku zu QwtPlotCurve::setRawSamples sagt aber:
It is important to keep the pointers during the lifetime of the underlying QwtCPointerData class.
Die übergebenen Zeiger müssen mindestens so lange leben, wie das QwtCPointerData-Objekt hinter der Curve - und das tun sie aus o.g. Grund nicht.
Lösung: x und y zu Membern machen.
-
Danke für die schnelle Antwort, läuft jetzt
Kann ich die Dicke des Graphen irgendwie verändern?MfG ch_1992
-
curve->setPen(QPen(Qt::red, 5)); //-->5 ist die Dicke der Linie/Graphen
-
Danke!
Sorry für solche Anfängerfragen.
Aber kann man auch iwie den Hintergrund des Plots ändern und gibt es iwo ne Liste der möglichen Farben?MfG ch_1992
-
Du solltest Dir auf jedenfall die Doku und die Examples dringend anschauen. Da ist alles dabei.
http://qwt.sourceforge.net/class_qwt_plot.html#adb0b2e68d86039f86e3240fb399fa0fe
in Deinem Beispiel:
plot.setCanvasBackground(Qt::darkblue);
Farben gehen alle die QColor zur Verfügung stellt, also (fast) alle.
-
Hallo,
noch eine Frage hab dazu in der Doku nichts gefunden.
Und zwar hab ich jetzt 6 Plots in einem Fenster,
von denen aber immer nur drei angezeigt werden und man mittels
eines Buttons die Ansicht umschalten kann.
Ich hab die Plots in einem QGridLayout und hab den Kolonnen und Reihen ne Mindestbreite und -höhe gegeben.
Wenn ich jetzt mittels Button die Ansicht wechselt, werden die Fenster noch größer, kann ich iwie in QGridLayout auch ne Maximalgröße angeben?MfG ch_1992