December 25, 2010

endla.ravi endla.ravi
Lab Rat
17 posts

Behavior of paint method in QGraphicsItem

 

hello,

when i inherit QGraphicsItem class and implement paint method,i am getting a problem,

the paint method is being called twice.What is the reason??

ie.

  1. void Mouse::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
  2. @{
  3.  
  4.    
printf(“i entered once:\n”);
@}

gives me the following output

i entered once:
i entered once:

What is the reason for this?And how can i avoid this…

5 replies

December 25, 2010

endla.ravi endla.ravi
Lab Rat
17 posts

/*this is the code*/

  1. #include <QtGui/QApplication>
  2. #include "mainwindow.h"
  3. #include "QGraphicsItem"
  4. #include "stdio.h"
  5. #include "QGraphicsScene"
  6. #include "QGraphicsView"
  7. class Draw:public QGraphicsItem
  8. {
  9.   public:
  10.     Draw();
  11.     QRectF boundingRect() const;
  12.     QPainterPath shape() const;
  13.     virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
  14. };
  15.  
  16. Draw::Draw()
  17. {
  18. }
  19.  
  20. QRectF Draw::boundingRect() const
  21. {
  22. }
  23.  
  24. QPainterPath Draw::shape() const
  25. {
  26. }
  27.  
  28. void Draw::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  29. {
  30.   printf("Entered once:\n");
  31. }
  32.  
  33. int main(int argc, char *argv[])
  34. {
  35.   QApplication a(argc, argv);
  36.   Draw *mouse=new Draw();
  37.   QGraphicsScene scene;
  38.   scene.setSceneRect(0, 0, 500, 500);
  39.   scene.setItemIndexMethod(QGraphicsScene::NoIndex);
  40.   mouse->setPos(10,10);
  41.   scene.addItem(mouse);
  42.  
  43.   QGraphicsView view(&scene);
  44.   view.setRenderHint(QPainter::Antialiasing);
  45.   view.setBackgroundBrush(QPixmap(":/images/cheese.jpg"));
  46.   view.setCacheMode(QGraphicsView::CacheBackground);
  47.   view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
  48.   view.setDragMode(QGraphicsView::ScrollHandDrag);
  49.   view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Colliding Mice"));
  50.   view.show();
  51.   return a.exec();
  52. }

Output
Entered once
Entered once

[EDIT: code formatting cleanup, Volker]

December 25, 2010

t3chNo t3chNo
Lab Rat
55 posts

i don’t think this is a problem. why do you bother this?

December 25, 2010

peppe peppe
Ant Farmer
1025 posts

Can you modify your previous posts and fix the code snippets? Just put a ‘@’ before the whole snippet, and another one after; not one on each line!

BTW, why do you care about this?

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

December 25, 2010

Immii Immii
Ant Farmer
233 posts

paint method will be called as many time as there is any need of referesh in your view, first its called when you are creating the object second time it will be called when you show it on your view. and it will be called many other time if you minized or maximized etc

December 25, 2010

Milot Shala Milot Shala
Lab Rat
393 posts

This article [thesmithfam.org] might help.

 
  ‹‹ [solved] How to set the changed text as default text? (for lineEdit)      [Solved] invalid conversion from ’char’ to ’char*’ ››

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