Results 1 to 2 of 2

Thread: Custom QGraphicsItem paint function problem

  1. #1
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Custom QGraphicsItem paint function problem

    Hello!

    I have a class that inherits QGraphicsItem and QObject:
    Qt Code:
    1. class Foo: public QObject, public QGraphicsItem
    2. {
    3. Q_OBJECT
    4. ...
    5. };
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    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. }
    To copy to clipboard, switch view to plain text mode 
    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!
    Last edited by andzero; 12th October 2011 at 16:25. Reason: missing information

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom QGraphicsItem paint function problem

    The pixmap will be switched each time the paint method is called, this is probably not what you want.
    Connect the timer to a slot, where you will switch the pixmap and update the item:
    Qt Code:
    1. /*slot*/
    2. void Foo::timeout(){
    3. if( this->_pixmap == pixmap1 ){
    4. this->_pixmap = pixmap2;
    5. } else{
    6. this->_pixmap = pixmap1;
    7. }
    8. update();
    9. }
    10.  
    11. void Foo::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    12. {
    13. painter->drawPixmap(bodyRect, this->_pixmap);
    14. }
    To copy to clipboard, switch view to plain text mode 
    Now on each timeout the pixmap will be switched, and item updated, no matter how often its rendered.
    Btw. If you are using Qt >= 4.6, take a look at the QGraphicsObject class.

  3. The following user says thank you to stampede for this useful post:

    andzero (13th October 2011)

Similar Threads

  1. Help on QGraphicsItem::paint please
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2011, 08:11
  2. Custom QStyledItemDelegate paint function never called
    By mattsnowboard in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2011, 01:57
  3. QGraphicsItem paint not triggered
    By roband915 in forum Qt Programming
    Replies: 9
    Last Post: 31st March 2011, 10:06
  4. Qgraphicsitem parent/child paint problem.
    By repka3 in forum Qt Programming
    Replies: 1
    Last Post: 24th July 2009, 22:03
  5. Paint QGraphicsItem problem
    By dreamer in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2008, 18:18

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.