PDA

View Full Version : paint



xyzt
22nd March 2008, 17:11
i try to learn QT and I wrote MyItem class inherits the QGraphicsItem class. i have added a line to the paint method to see what is going on while runnig. i saw that it wrotes the string twice.i couldnt understand why...
here is my code:



#ifndef MYITEM_H_
#define MYITEM_H_
#include <QGraphicsItem>
class MyItem:public QGraphicsItem
{
public:
MyItem();
virtual ~MyItem();

QRectF boundingRect() const;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *e);
void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );

};

#endif /*MYITEM_H_*/


#include "MyItem.h"
#include <QtGui>
MyItem::MyItem():
QGraphicsItem(0,0)
{

}

MyItem::~MyItem()
{
}

void MyItem::mousePressEvent(QGraphicsSceneMouseEvent* e){

QGraphicsItem::mousePressEvent(e);
}

void MyItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
{
painter->drawEllipse(100,100,20,20);
qDebug("testing"); // it wrotes the string twice....
}

QRectF MyItem::boundingRect() const
{

}

wysota
22nd March 2008, 17:33
You must have done something that caused a repaint of the item. This is nothing to worry about. If you wish to change it, try reordering calls to the view, scene and item. In particular make sure you first add the item to the scene and then set the scene on the view, not the other way round.

xyzt
22nd March 2008, 18:16
You must have done something that caused a repaint of the item. This is nothing to worry about. If you wish to change it, try reordering calls to the view, scene and item. In particular make sure you first add the item to the scene and then set the scene on the view, not the other way round.
i do as you say: add the object to the scene and set the scene for the view but it prints the "testing" string twice...


QGraphicsScene scene(0,0,200,200);
scene.addItem(new MyItem());
QGraphicsView view(&scene);
view.show();
return a.exec();

wysota
22nd March 2008, 18:22
Maybe you change some properties of the view or something? You might search for recent posts of user "Bitto", I remember him solving such a problem like yours (or at least describing what happens).