Results 1 to 5 of 5

Thread: Is it possible to use QPainter with QGraphicsItem?

  1. #1
    Join Date
    Jan 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Question Is it possible to use QPainter with QGraphicsItem?

    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!

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Is it possible to use QPainter with QGraphicsItem?

    Every QGraphicsItem has a special virtual method QGraphicsItem::paint(). QPainter is passed as an argument. Take a look at docs, there's an example.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Default Re: Is it possible to use QPainter with QGraphicsItem?

    Could you give me an example?
    If I create a QGraphicsPixmapItem, how can i draw inside the item freehand lines?
    Thanks!

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Is it possible to use QPainter with QGraphicsItem?

    • 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.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    william.dias (8th February 2008)

  6. #5
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 38 Times in 30 Posts

    Default Re: Is it possible to use QPainter with QGraphicsItem?

    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.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  7. The following user says thank you to Bitto for this useful post:

    william.dias (8th February 2008)

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 11:31
  2. rotation of an animated QGraphicsItem
    By darksaga in forum Qt Programming
    Replies: 9
    Last Post: 9th September 2007, 09:11
  3. Force the painting of a QGraphicsItem
    By fabietto in forum Qt Programming
    Replies: 3
    Last Post: 2nd July 2007, 22:28
  4. QPainter and QImage
    By beerkg in forum Newbie
    Replies: 3
    Last Post: 7th September 2006, 15:48
  5. Replies: 7
    Last Post: 20th March 2006, 21:03

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
  •  
Qt is a trademark of The Qt Company.