PDA

View Full Version : How to write a derived class of QwtSymbol?



momo
11th February 2014, 07:35
Hi all,

I meet a problem with QwtSymbol, I want to design some symbols so I have to write a derived class of QwtSymbol,
but It seems like that I need to access the PrivateData of parent class, and the Enumerated type Style of parent class doesn't include the style I designed, how could I write this derived class?

Please excuse me for my poor English, I am not good at it, but I am trying to improve it.

Momo

Uwe
11th February 2014, 17:44
You can use symbols types >= QwtSymbol::UserStyle + overloading QwtSymbol::renderSymbols(). But I would expect that the implementation you are looking for is to use one of the symbol types >= QwtSymbol::Path.

Uwe

momo
12th February 2014, 01:52
Hi Uwe,
Thank you for your reply. My team use Qwt 6.0.2 to develop so I can not use QwtSymbol::Path.
In my derived class CSymbol I wrote an enum Style that added a TestSymbol after UserStyle, now I meet two questions:
1. How to write CSymbol's Constructors? QwtSymbol's constructors need QwtSymbol::Style in theirs first parameter, but I have to use CSymbol::Style.
2. In QwtSymbol::renderSymbols() (It called drawSymbols() in Qwt6.0.2) it accessed d_data which is private in parent class, and one of the d_data's memeber is QwtSymbol::Style, but I have to use CSymbol::Style. Should I write a new d_data?

Here is Symbol.h's code:


#ifndef SYMBOL_H_
#define SYMBOL_H_

#include <qwt/qwt_symbol.h>

class CSymbol : public QwtSymbol
{
public:
enum Style
{
//! No Style. The symbol cannot be drawn.
NoSymbol = -1,

//! Ellipse or circle
Ellipse,

//! Rectangle
Rect,

//! Diamond
Diamond,

//! Triangle pointing upwards
Triangle,

//! Triangle pointing downwards
DTriangle,

//! Triangle pointing upwards
UTriangle,

//! Triangle pointing left
LTriangle,

//! Triangle pointing right
RTriangle,

//! Cross (+)
Cross,

//! Diagonal cross (X)
XCross,

//! Horizontal line
HLine,

//! Vertical line
VLine,

//! X combined with +
Star1,

//! Six-pointed star
Star2,

//! Hexagon
Hexagon,

/*!
Styles >= QwtSymbol::UserSymbol are reserved for derived
classes of QwtSymbol that overload drawSymbols() with
additional application specific symbol types.
*/
UserStyle = 1000,

TestSymbol
};

public:
// How to write this Constructors?
CSymbol( Style style = NoSymbol ) : QwtSymbol(style) {};
CSymbol( Style style, const QBrush &brush, const QPen &pen, const QSize &size )
: QwtSymbol(style, brush, pen, size) {};
CSymbol( const CSymbol &symbol ) : QwtSymbol(symbol) {};
virtual ~CSymbol() {~QwtSymbol(); };

protected:
virtual void drawSymbols( QPainter *,
const QPointF *, int numPoints ) const;
}; // SYMBOL_H_
#endif


Here is CSymbol::drawSymbols()'s code:


void CSymbol::drawSymbols( QPainter *painter,
const QPointF *points, int numPoints ) const
{
if ( numPoints <= 0 )
return;

painter->save();

// here is accessing QwtSymbol::d_data
switch ( d_data->style )
{
case CSymbol::Ellipse:
{
qwtDrawEllipseSymbols( painter, points, numPoints, *this );
break;
}
case CSymbol::Rect:
{
qwtDrawRectSymbols( painter, points, numPoints, *this );
break;
}
case CSymbol::Diamond:
{
qwtDrawDiamondSymbols( painter, points, numPoints, *this );
break;
}
case CSymbol::Cross:
{
qwtDrawLineSymbols( painter, Qt::Horizontal | Qt::Vertical,
points, numPoints, *this );
break;
}
case CSymbol::XCross:
{
qwtDrawXCrossSymbols( painter, points, numPoints, *this );
break;
}
case CSymbol::Triangle:
case CSymbol::UTriangle:
{
qwtDrawTriangleSymbols( painter, QwtTriangle::Up,
points, numPoints, *this );
break;
}
case CSymbol::DTriangle:
{
qwtDrawTriangleSymbols( painter, QwtTriangle::Down,
points, numPoints, *this );
break;
}
case CSymbol::RTriangle:
{
qwtDrawTriangleSymbols( painter, QwtTriangle::Right,
points, numPoints, *this );
break;
}
case CSymbol::LTriangle:
{
qwtDrawTriangleSymbols( painter, QwtTriangle::Left,
points, numPoints, *this );
break;
}
case CSymbol::HLine:
{
qwtDrawLineSymbols( painter, Qt::Horizontal,
points, numPoints, *this );
break;
}
case CSymbol::VLine:
{
qwtDrawLineSymbols( painter, Qt::Vertical,
points, numPoints, *this );
break;
}
case CSymbol::Star1:
{
qwtDrawStar1Symbols( painter, points, numPoints, *this );
break;
}
case CSymbol::Star2:
{
qwtDrawStar2Symbols( painter, points, numPoints, *this );
break;
}
case CSymbol::Hexagon:
{
qwtDrawHexagonSymbols( painter, points, numPoints, *this );
break;
}
case CSymbol::TestSymbol:
{
qwtDrawTestSymbols( painter, points, numPoints, *this );
break;
}
default:;
}
painter->restore();
}


Momo

Uwe
12th February 2014, 18:11
Simply start with your enum ( no need to copy the QwtSymbol symbol types ) from QwtSymbol::UserStyle and use c++ casts. In the end an enum is just a number and it is completely safe to cast between them.

Uwe

momo
13th February 2014, 02:35
Problem solved. Thank you Uwe!

Momo