PDA

View Full Version : How can I get a custom QCanvasItem?



luffy27
20th April 2007, 20:09
hello guys:
I want to define my own CanvasItem to draw on the QCanvas. Maybe a coordinate(maybe a class named Coordinate) with a X direction axis(which can be a QCanvasLine, I think) and a Y direction axis. And also some texts around the two axes.
That means the custom class Coordinate contains some properties like QCanvasLine, QCanvasText, QCanvasEllipse and some member functions in order to change these properties. And then I can draw the whole Coordinate by only one step on the canvas instead of adding each lines or texts everytime.

As the Qt document says, if you want a custom canvasitem, you can subclass the QCanvasPolygonalItem. I have tried this method, but it doesn't work. I think the QCanvasPolygonalItem maybe can not contain other QCanvasItem like QCanvasLine as its properties because it is also subclassed from QCanvasItem. Is that right?

But now I cannot figure out how to achieve my goal. Help me please. Thanks a lot.

Best regards.

marcel
20th April 2007, 22:57
Why don't you subclass QCanvasItem directly and in QCanvasItem::draw() you do all the things you mentioned, like drawing a line, texts, an ellipse, etc...

You could add QPaintePath members to your item to describe the line and ellipse, and paint them in draw( QPainter* ).

Regards.

luffy27
21st April 2007, 19:36
Is there any examples for this method? Could you offer me some links or some materials in Qt Documents?

And as the Qt's Documents say:
"QCanvasItem is not intended for direct subclassing. It is much easier to subclass one of its subclasses, e.g. QCanvasPolygonalItem (the commonest base class), QCanvasRectangle, QCanvasSprite, QCanvasEllipse or QCanvasText."
What is the difference between subclassing from QCanvasItem and from QCanvasPolygonalItem?

Thanks a lot.

marcel
21st April 2007, 19:42
Is there any examples for this method? Could you offer me some links or some materials in Qt Documents?

I don't know of any examples out there, but you can always take a look in the Qt sources. For example you could look at the implementations of QCanvasPolygonalItem and it's subclasses, like QCanvasEllipse.

This should give you an idea on how to do things.

Basically you will draw more than one canvas item in your draw method. For this, in your subclass of QCanvasItem you will have members for all the shapes you have to draw, like lines, ellipses, etc...
All you have to do is draw these in the draw method.

Also:


Warning: When you reimplement this function, make sure that you leave the painter in the same state as you found it. For example, if you start by calling QPainter::translate (http://doc.trolltech.com/3.3/qpainter.html#translate)(50, 50), end your code by calling QPainter::translate(-50, -50). Be also aware that the painter might already have some transformations set (i.e., don't call QPainter::resetXForm (http://doc.trolltech.com/3.3/qpainter.html#resetXForm)() when you're done).


Regards

marcel
21st April 2007, 19:52
"QCanvasItem is not intended for direct subclassing. It is much easier to subclass one of its subclasses, e.g. QCanvasPolygonalItem (the commonest base class), QCanvasRectangle, QCanvasSprite, QCanvasEllipse or QCanvasText."
What is the difference between subclassing from QCanvasItem and from QCanvasPolygonalItem?


In case you need to draw just an item, then you subclass QCanvasPolygonalItem.

But your need is more complex, you need to draw multiple items - lines, ellipse, text. In this case you subclass QCanvasItem because it will offer you more flexibility, especially in the way you draw items.

Regards