PDA

View Full Version : Why QGraphicsItem moveing is valid in half?



litterflybug
13th December 2009, 09:52
Hello everyone
i subclass QGraphicsItem, in constractor,i add QGraphicsPixmapItem,and a
QGraphicsTextItem. When drag this item
Why drag is an effective second half, while the upper part of the void?

this is my code :



YPhoto::YPhoto(QPixmap&pix,QString&info,QGraphicsItem * parent)
:QGraphicsItem(parent)
{
photo=new QGraphicsPixmapItem(this);
photo->setPixmap(pix);
baget=new QGraphicsTextItem(this);
//name=new QGraphicsTextItem(this);
//email=new QGraphicsTextItem(this);

QTextDocument* document=new QTextDocument(info);
baget->setDocument(document);
QRect rect=photo->pixmap().rect();
baget->setTextWidth(rect.width());
baget->setPos(this->pos().x(),this->pos().y()+rect.height());

}

QRectF YPhoto::boundingRect() const
{
QRect rect=photo->pixmap().rect();
QFontMetrics fm(baget->font());

rect.setHeight(rect.height()+fm.height());
return QRectF(rect);
}

QPainterPath YPhoto::shape() const
{
QPainterPath path;
QRect rect=photo->pixmap().rect();
path.addRect(rect);
path.addPath(baget->shape());
return path;
}

void YPhoto::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawPixmap(option->exposedRect,photo->pixmap(),option->exposedRect);
}

wysota
13th December 2009, 10:00
You are doing it all wrong, so it seems. If you have a pixmap item as a child of your main item then you shouldn't try to draw its pixmap in the main item's painting routine - the child item will draw it. Your main item should just be a QGraphicsItemGroup containing two child items, no need to subclass anything even.

litterflybug
13th December 2009, 14:14
thank you advice,this is very help,i will view document and try it again

Thanks again, i use QGraphicsItemGroup,it worked fine