Results 1 to 5 of 5

Thread: How can I get a custom QCanvasItem?

  1. #1
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default How can I get a custom QCanvasItem?

    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I get a custom QCanvasItem?

    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.

  3. #3
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How can I get a custom QCanvasItem?

    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.
    Last edited by luffy27; 21st April 2007 at 18:41.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I get a custom QCanvasItem?

    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(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() when you're done).
    Regards

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I get a custom QCanvasItem?

    "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

Similar Threads

  1. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28
  2. Replies: 1
    Last Post: 5th March 2007, 20:50
  3. Custom Widget Plugin Analog Clock
    By kemp in forum Qt Programming
    Replies: 8
    Last Post: 11th August 2006, 13:22
  4. Custom plugin for a layout
    By cocheci in forum Qt Tools
    Replies: 2
    Last Post: 12th June 2006, 18:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.