PDA

View Full Version : qwtPlot->replot() results in segmentation fault



kaizimir
24th August 2009, 14:08
Hi all,

trying to plot in a Qt4 mainwindow, which was layouted with Qt designer. So I have a Ui_MainWindow class, which declares and defines QwtPlot *qwtPlot.

The Ui_MainWindow class is privately inherited by another class, lets call it PlotOri, in order to have access to all the graphical elements.

Here is the part of the constructor of PlotOri, where I try to use the replot() methode:



const double* t = times;
const double* r = recordings;

QwtArrayData qwtArray(t, r, 10);

// add curves
QwtPlotCurve *curve1 = new QwtPlotCurve("Curve 1");

// copy the data into the curves
curve1->setData(qwtArray);

curve1->attach(qwtPlot);

// finally, refresh the plot
qwtPlot->replot();

At runtime I receive a 'Segmentation fault' and the program crashes. Commenting the last line prevents from crashing, but of course I also don't see a plot.

Maybe it is just a newbie's mistake. I hope you can help me and thx in advance.

greetz, kai

jmsbc
24th August 2009, 19:30
Is qwtPlot protected or private?

kaizimir
24th August 2009, 19:40
Hmmm, it's public... I can't guess what difference it makes.?

Ok, I also tried the public inheritance. Nothing changes.

Then I put the code from the first post into the 'Ui_MainWindow' class. It works. This of course not what I want to code. But I am puzzled, what that could mean.

Any help is highly appreciated.

greetz, kai

kaizimir
26th August 2009, 20:44
Tried it again on another machine. Same result. From that I conclude, it is not a problem with the memory.

Ok, let me ask another question: Is it possible to invoke a methode of an qwt object, in this case qwtplot, from within another class, in which the qwt object was not declared or defined? This other class will inherit the class in which the qwt object was declared and defined. So all methodes are available, right?

greetz, kai

franz
26th August 2009, 20:55
Can you show a stack trace from your debugger? That will help finding the problem a great deal.

jmsbc
26th August 2009, 21:41
Tried it again on another machine. Same result. From that I conclude, it is not a problem with the memory.

Ok, let me ask another question: Is it possible to invoke a methode of an qwt object, in this case qwtplot, from within another class, in which the qwt object was not declared or defined? This other class will inherit the class in which the qwt object was declared and defined. So all methodes are available, right?

greetz, kai
Well if qwtPlot was never instantiated (new'ed) before qwtPlot->replot() is called, then it will crash

kaizimir
26th August 2009, 23:22
Hey,
I am happy you are still there. First I am sorry if my terminology is not completly correct (I am new to all this): The object qwtPlot is 'newed' and I also see it in the mainwindow as central widget.

Second I am not sure what a stack trace is. I uploaded a screen shot of what I think is the stack trace on rapidshare:
http://rapidshare.com/files/271899309/stackTrace.png.html
Unfortunately KDBG and KDevelop have no copy&paste for this.

I substituted the data, which are loaded from a file and should be plotted, with very short arrays of just 10 values. I was suspicious, that my data might be the problem. But the problem remains. If I put the arrays in the class, where qwtPlot is instantiated, it is working fine.

Thanks for your patience,
kai

emrares
27th August 2009, 06:27
had the same problem because my scale had invalid ticks

kaizimir
27th August 2009, 07:42
had the same problem because my scale had invalid ticks

I think it is not the same problem, because the problem as I described it is independent from the data.

ProTonS
27th August 2009, 20:13
hello man,

Try to do it:

ui.qwtPlot->replot(); <-- default vc++

or

ui->qwtPlot->replot(); <-- default mingw

hmmm, try to make QwtPlotCurve *curve1 global too ^^

QwtPlotCurve *curve1; in header and

curve1 = new QwtPlotCurve(); in .cpp

bye bye

kaizimir
28th August 2009, 20:31
Hey guys,

I am really desperate. Therefore I reconstructed my problem in a plain new project.

This is the constructor of the 'mainwindow' class, which is called by main:


#include <QObject>
#include <QMainWindow>
#include <QtGui>

#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_data.h>

#include "ui_GUI_mainwindow.h"
#include "m_mainwindow.h"
#include "m_plot.h"

M_MainWindow::M_MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);

double x[] = {1, 2, 3, 4, 5};
const double* x_ptr = x;
const double* y_ptr = x;

QwtArrayData qwtArray ( x_ptr, y_ptr, 5 );

QwtPlotCurve *curve1 = new QwtPlotCurve ( "Curve 1" );

curve1->setData ( qwtArray );

curve1->attach( qwtPlot );

qwtPlot->replot();

// M_Plot qPlot; // <- HERE IS THE PROBLEM

}

The line is plotted in the qwt widget as expected. If I include:
'M_Plot qPlot;' the program crashes and I receive the 'segmentation fault' error. Compiling the program is not a problem.

This is the constructor for the M_Plot class:

#include <QObject>
#include <QMainWindow>

#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_data.h>

#include "ui_GUI_mainwindow.h"
#include "m_plot.h"

M_Plot::M_Plot()
{
double x[] = {2, 4, 6, 8, 10};
const double* x_ptr = x;
const double* y_ptr = x;

QwtArrayData qwtArray ( x_ptr, y_ptr, 5 );

QwtPlotCurve *curve1 = new QwtPlotCurve ( "Curve 1" );

curve1->setData ( qwtArray );

curve1->attach( qwtPlot );

qwtPlot->replot();
}

I really don't know what I am doing wrong. Maybe I am already blinded and can't find an obvious mistake.

I posted a stack trace in a previous post yesterday.

System specs:
openSUSE 11.1
qwt 5.2.0 -> from the repositories
Qt 4.4.3
gcc-c++ 4.3
KDevelop 3.5.3

greetz, kai

Edit: I just compiled qwt 5.2.0 from Sourceforge and used it in the program, but also didn't help. Damn it :mad:

jmsbc
29th August 2009, 00:59
Where is qwtPlot instantiated? Did u add it using the designer? If so, why isn't it ui.qwtPlot?

kaizimir
29th August 2009, 09:26
Here is the class generated by the designer, which instantiats qwtPlot:


QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
QWidget *centralwidget;
QwtPlot *qwtPlot; // here
QMenuBar *menubar;
QStatusBar *statusbar;

void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(800, 600);
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
qwtPlot = new QwtPlot(centralwidget); // here
qwtPlot->setObjectName(QString::fromUtf8("qwtPlot"));
qwtPlot->setGeometry(QRect(40, 30, 721, 501));
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 800, 24));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);

retranslateUi(MainWindow);

QMetaObject::connectSlotsByName(MainWindow);
} // setupUi

void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
Q_UNUSED(MainWindow);
} // retranslateUi

};

namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE



And this is the header of the M_MainWindow class I posted yesterday:


class M_MainWindow : public QMainWindow,
private Ui::MainWindow
{
Q_OBJECT

public:
M_MainWindow ( QWidget *parent = 0 );

~M_MainWindow();

};


I think, the private Ui::MainWindow inheritance is the reason, why I don't need the ui.*...

I am really sorry to get on your nervs every day. But it is a real pity to have such a show stopper as a newby to C++, Qt and qwt when everything else works fine.

kaizimir
4th September 2009, 12:42
Hey,

I found the problem and I must admitt, that it is not related to qwt.

@jmsbc: You were right from the beginning, that it is a problem of instantiation and that the plot widget isn't accessible from where I wanted to use it.

I am doing it now by handing over a pointer to the qwt widget to the class that uses the plot. I am not very happy with that solution and hope to find a way with inheritance.

Thanks to all,

kai

gudlaugu
24th October 2009, 04:37
I know I am a bit late to this thread, but you forgot to call setupUi(this) in the constructor of M_Plot.

windsword
26th October 2009, 18:47
this might be somewhat related, I found that replot() works well when I rescale my x-axis in Mac OS platform, but it does not respond/work when I use this in Linux? It's kind of strange. I wonder if this is related?

I just posted the details in a separate thread.