PDA

View Full Version : Is it possible to use QPainter with QGraphicsItem?



william.dias
31st January 2008, 08:09
Hi!

I am trying to create a generic QGraphicsItem and then use the QPainter to draw rectangles, circles and freehand lines inside the item. Does any body know if it is possible? Could you give me a exemple? If it is not, does any body have an alternative way to do this?

I am using QT 4.3 and Windows XP.

Thanks!

jpn
31st January 2008, 08:35
Every QGraphicsItem has a special virtual method QGraphicsItem::paint(). QPainter is passed as an argument. Take a look at docs, there's an example.

william.dias
31st January 2008, 09:58
Could you give me an example?
If I create a QGraphicsPixmapItem, how can i draw inside the item freehand lines?
Thanks!

jpn
31st January 2008, 11:56
subclass/inherit (which ever term you're familiar with) QGraphicsPixmapItem just like you would subclass any class in C++
reimplement/override (again, which ever term you're familiar with) paint() just like you would reimplement any virtual method in C++, see docs for exact function signature
in reimplemented paint(), call the base class implementation to paint the pixmap and draw your custom lines afterwards using the painter passed as a parameter

Things like inheritance and member function overriding are not covered by Qt docs. They are basic OOP concepts one should know before one starts working with Qt at all.

Bitto
2nd February 2008, 10:19
William Dias: Have you tried running the Chip Demo, then holding the secret Shift-button and then clicking and dragging on a chip?

This shows one way to draw freehand on an item. It's one practical approach with source code (chip.cpp) - the item tracks all mouse positions and generates a path by connecting the points.