October 12, 2011

andzero andzero
Lab Rat
7 posts

[Solved] Custom QGraphicsItem paint problem



I have a class that inherits QGraphicsItem and QObject:

  1. class Foo: public QObject, public QGraphicsItem
  2. {
  3.    Q_OBJECT
  4.    ...
  5. };

I have set up a QTimer, and connects its timeout signal with QGraphicsScene’s advance.

I want to display 2 constantly shifting QPixmap on a Foo instance when it is shown:

  1. void Foo::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  2. {
  3.     painter->drawPixmap(bodyRect, animate?pic1:pic2); // animate is a boolean variable.
  4.     if(animate) animate = false;
  5.     else animate = true;
  6. }

e.g., imagine a character walking animation with left foot pixmap and right foot pixmap.

My problem is, the result shown is not as what i have expected.
it looks like pic1 is drawn above pic2 or vice versa.
( pic1 and pic2 is a .png file, supports transparent background. does that have any effect :? )

is it impossible to make this, via paint function re-implementation?
(i’m doing this for learning purposes)

Thanks for your help and attention!

1 reply

December 9, 2011

andzero andzero
Lab Rat
7 posts

Solved! I used update() in a separate function/ slot.

Thank you! :)

  ‹‹ Problem in deleting QTabWidget’s Widget...      Qt LGPL and EPL compatibility ››

You must log in to post a reply. Not a member yet? Register here!