PDA

View Full Version : QGraphicsItem paint problem



mvbhavsar
9th February 2015, 09:53
Hello,

I have following files

architem.h
=========



#ifndef ARCITEM_H
#define ARCITEM_H

#include <QtWidgets>

class ArcItem : public QGraphicsItem
{
public:
ArcItem(QGraphicsItem *parent=0);
~ArcItem();

QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void setRect(QRectF rct);

protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);

private:
QPainterPath path;
QRectF arcRect;
int startAngle;
int spanAngle;

};

#endif // ARCITEM_H



arcitem.cpp
=========



#include "arcitem.h"

ArcItem::ArcItem(QGraphicsItem *parent): QGraphicsItem(parent),
arcRect(0,0,100,100),
startAngle(1*16),
spanAngle(200*16)
{
setFlags(ItemIsSelectable|ItemIsMovable|ItemSendsG eometryChanges);
setData(0,"ArcItem");
}

ArcItem::~ArcItem()
{

}

QVariant ArcItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
return QGraphicsItem::itemChange(change, value);
}



QRectF ArcItem::boundingRect() const
{
return arcRect;
}

void ArcItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->save();
painter->drawArc(arcRect,startAngle,spanAngle);
painter->restore();
}

void ArcItem::setRect(QRectF rct)
{
arcRect = rct;
update(rct);
}




When I am resizing this item using handles, it show uncleared lines on scene as the picture attached.
Actually, boundingrect is set from outside of this class by calling setRect custom method in this class.
Although item gets resized, it behaves as per attachment.
Any help on this?


Thanks

Manish

anda_skoa
9th February 2015, 11:23
The new rect is not necessarly encompassing the old rect, right?

So calling update with just the new rect might leave area unupdated that was part of the previous rect.

Maybe call update with the union of the old and the new rect?

Cheers,
_

mvbhavsar
9th February 2015, 11:35
Thanks Anda.
I did that as well but still the problem persists. I have infact updated bigger viewport as well.

anda_skoa
9th February 2015, 14:17
Is your item the only item showing that problem?

Have you tried without the ItemSendsGeometryChanges flag?

Cheers,
_

wysota
9th February 2015, 18:26
I don't see you calling prepareGeometryChange() after the bounding rect is changed.

mvbhavsar
10th February 2015, 08:06
Thanks Wysota.
Yes, prepareGeometryChange() has made the trick. Now it is working well and the problem is solved.


Thanks

Manish