PDA

View Full Version : Custom QwtPlot class



Carlton
4th September 2010, 18:05
I am attempting to create my own custom QwtPlot class, and use a static_cast to override the existing QwtPlot object in the programs dialog.
The dialog has a QwtPlot object called plotpage, I need to exchange my own variable called plot from my class myQwtPlot


QwtPlot* plot = static_cast<myQwtPlot*>(plotpage);
if(!plot)qFatal("Failed to cast the plot pointer from myQwtPlot to QwtPlot");
plot->setTitle("Account Balance Plot");

The code above is extracted from the dialog class constructor and appears to run correctly. When I attempt to use the plot variable in other member functions I get a SIGSEGV failure. The program is still expecting the original plotpage variable.
Could some one confirm I am using the correct approach, and have the correct syntax for the static_cast command.
Thanks,
Carlton.

Uwe
5th September 2010, 13:30
Without knowing about the declaration/assignment of the plotpage variable nobody can tell you if your code is correct. IMO the only reasonable situation where your cast makes sense is something like this:


QWidget *plotpage = plot1; // something derived from QwtPlot
// ...
QwtPlot *plot2 = dynamic_cast<QwtPlot *>(plotpage);
As long as you don't have rtti disabled don't use static_casts for situations like this.

Uwe

Carlton
5th September 2010, 18:37
plotpage is declared in the dialog header file (generated by Creator) as


QtPlot *plotpage;

In your example above, plot2 would also be a QwtPlot object, I need it as a myQwtPlot object to enable my own class functions to override the QwtPlot functions.
myQwtPlot is defined as follows;


lass myQwtPlot : public QwtPlot
{
Q_OBJECT
public:
explicit myQwtPlot(QWidget *parent = 0);

signals:

public slots:

}
Since there is only a single item to cast at the start of the program, I didn't think I would need a dynamic_cast, and avoid the need for rtti
Carlton.

Uwe
6th September 2010, 06:57
Ok, plotpage is declared as QtPlot - but what is this ?

If it is something derived from QwtPlot you don't need to cast at all. If not your cast is very, very wrong and all it does is to convert a compile error into a runtime error.

Again your cast makes only sense when plotpage is declared as a baseclass of QwtPlot ( QObject, QWidget, QFrame ) or maybe a void * - and of course a object of a class derived from QwtPlot has been assigned to it.

Uwe

Carlton
6th September 2010, 12:46
plotpage is a QwtPlot object, I don't know how the 'w' dropped out on the cut & paste earlier.


QwtPlot *plotpage;

The intention is to convert the existing plotpage object ( class QwtPlot ) to my own derived class myQwtPlot.

Uwe
6th September 2010, 12:57
But when the result is assigned to a local variable, that is declared as QwtPlot *, the const_cast is pointless.

Uwe

Carlton
6th September 2010, 14:02
The intention was to keep using the base class in the .ui file, then cast it to my derived class to enable me
to use my own version of QwtPlot, so I can override the picker and zoom functions.

Uwe
6th September 2010, 14:31
But this is not what the the code does, that you have posted !

Also QwtPlot has no picker or zoom functionality. If you want to manipulate zooming/picking you have to derive from QwtPlotZoomer, QwtPlotPicker.

Uwe

Carlton
6th September 2010, 14:44
Thanks for the help, it is much appreciated. I shall return to the manual again.