Qt World Summit: Submit your Presentation

[Solved] drawing sympols

  • hello all;

    this is what i want to do:

    1. drawing a circle/polygon with a letter inside my circle/polygon
    2. drawing a line between my symbols
    3. i want to label my circle with a number, the label gonna be drawn on top of my circle/polygon
    4. i want to be able to drag/drop my symbols

    so what do you think is the best way of implementing it with qtcreator? shall i use QGraphicsObject or QPaint directly ?

  • Hi,
    I think you have to use QGraphicsScene !
    So, subclass QGraphicsScene to implement the drag/drop protected functions. You can also go to the Qt examples in your Qt installation directory and study the diagramScene example.

    In the other hand, to draw custom circles and polygons items, you have to subclass QGraphicsEllipseItem and QGraphicsPolygonItem and to reimplement those methods:
    virtual QRectF boundingRect() const;
    virtual QPainterPath shape () const;
    virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget );

    drawing label inside or in top of an item is easy. You can use QGraphicsSimpleTextItem :
    MyCircleItem::MyCircleItem () : QGraphicsEllipseItem()
    setRect(0, 0, 100, 100);

    label = new QGraphicsSimpleTextItem(this);
    label->setPos(20, 20);

    This will draw the label as a QGraphicsSimpleTextItem inside the Circle Item when the paint() method is called :)

  • you are awesome :D
    thank you so much issam thats what i really need;

  • good :)
    Try to find a way to add a [Solved] tag to the title :)

  • bq. Try to find a way to add a [Solved] tag to the title :)

    For the record, you can do that by editing your initial post and modifying the thread title.

Log in to reply