Results 1 to 2 of 2

Thread: PaintEvent on a Widget doesn't work on QGraphicsScene?

  1. #1
    Join Date
    May 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default PaintEvent on a Widget doesn't work on QGraphicsScene?

    Hey there,
    I was trying to use a QPaintEvent to paint QList<QPixmap>, an animation sprite and then add it QGraphicsScene. It did paint the animation when I run it however it did not seem to paint it into the scene. Can someone help me to solve this dilemma? Here's the code:
    Qt Code:
    1. //header
    2. #ifndef MISSLE1_H
    3. #define MISSLE1_H
    4.  
    5. #include <QGraphicsItem>
    6. #include <QPaintEvent>
    7. #include <QList>
    8. #include <QPoint>
    9. #include <QWidget>
    10. #include <QTimer>
    11.  
    12. class Missle1 : public QWidget
    13. {
    14. Q_OBJECT
    15. public:
    16. Missle1();
    17. void paintEvent(QPaintEvent *);
    18. // void changeDir();
    19. public Q_SLOTS:
    20. void nextFrame();
    21. private:
    22. int currentFrame;
    23. QList<QPixmap> RocketFrame;
    24. QList<QPixmap> *mCurrentSprites;
    25. QPoint mPos;
    26. QTimer *TimeFrame;
    27. };
    28.  
    29. //C++
    30. #include "Missle1.h"
    31. #include <QTimer>
    32. #include <QPaintEvent>
    33. #include <QPainter>
    34.  
    35. Missle1::Missle1()
    36. {
    37. RocketFrame.append(QPixmap(":/MissleTest1.png"));
    38. RocketFrame.append(QPixmap(":/MissleTest2.png"));
    39. RocketFrame.append(QPixmap(":/MissleTest3.png"));
    40. mCurrentSprites = &RocketFrame;
    41. currentFrame = 0;
    42. TimeFrame = new QTimer(this);
    43. TimeFrame->start(50);
    44. connect(TimeFrame,SIGNAL(timeout()),this,SLOT(nextFrame()));
    45.  
    46. }
    47.  
    48. void Missle1::paintEvent(QPaintEvent *)
    49. {
    50. QPainter painter(this);
    51. painter.drawPixmap(0,0,(*mCurrentSprites)[currentFrame]);
    52. }
    53.  
    54. void Missle1::nextFrame()
    55. {
    56. currentFrame = ++currentFrame % 3;
    57. repaint();
    58. }
    To copy to clipboard, switch view to plain text mode 

    The Widget which handles QGraphicScene and QGraphicsView

    Qt Code:
    1. //header
    2. #ifndef WIDGET_H
    3. #define WIDGET_H
    4.  
    5. #include <QWidget>
    6. #include "QtGui"
    7. #include "Missle1.h"
    8.  
    9. class Widget : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit Widget(QWidget *parent = 0);
    14. ~Widget();
    15.  
    16. private:
    17. Missle1 *rocket;
    18.  
    19. };
    20.  
    21. #endif // WIDGET_H
    22.  
    23. //C++
    24. #include "widget.h"
    25. #include "QtGui"
    26. //#include "ui_widget.h"
    27.  
    28. Widget::Widget(QWidget *parent) :
    29. QWidget(parent)
    30. {
    31.  
    32. //QPushButton *button = new QPushButton("Hello");
    33. scene = new QGraphicsScene(this);
    34. rocket = new Missle1();
    35. view = new QGraphicsView(scene,this);
    36. scene->setSceneRect(0,0,300,300);
    37. scene->addWidget(rocket);
    38. view->show();
    39.  
    40. }
    41.  
    42. Widget::~Widget()
    43. {
    44.  
    45. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PaintEvent on a Widget doesn't work on QGraphicsScene?

    Try showing the Missle1-widget:
    Qt Code:
    1. rocket->show();
    To copy to clipboard, switch view to plain text mode 

    Also, you could try to give the missle1 a minimum width and height.

Similar Threads

  1. Replies: 1
    Last Post: 11th March 2011, 19:34
  2. QGraphicsScene in paintEvent ?
    By PLan2010 in forum Newbie
    Replies: 5
    Last Post: 14th June 2010, 12:16
  3. why QT layout doesn't work on the second widget?
    By Kevin Hoang in forum Qt Programming
    Replies: 9
    Last Post: 20th March 2010, 21:29
  4. How to paint a widget outside paintEvent()
    By wesley in forum Qt Programming
    Replies: 10
    Last Post: 27th February 2008, 03:19
  5. painting a widget outside a paintEvent
    By jayw710 in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 23: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.