I have following problem:
I have a BallItem class derived from QGraphicsItem.
When I update position of my BallItem, Ballitem is flickering and sometimes in the screen (QGraphicsView) there are some remains left.
I attach source code of overrode methods:
BallItem
::BallItem(qreal aPosX, qreal aPosY, qreal aRadius,
QGraphicsItem *aParent
) : iVx(0),iVy(0),iRadius(aRadius)
{
setPos(aPosX,aPosY);
}
QRectF BallItem
::boundingRect() const {
qreal adjust = 0.5;
QRectF rectf
= QRectF(-iRadius
-adjust,
-iRadius
-adjust,iRadius
*2 + adjust
,iRadius*2 + adjust);
return rectf;
}
{
painter->save();
painter->setBrush(Qt::red);
painter
->drawEllipse
(QPointF(0,
0),iRadius,iRadius
);
painter->restore();
}
BallItem::BallItem(qreal aPosX, qreal aPosY, qreal aRadius,QGraphicsItem *aParent) :
QGraphicsItem(aParent),
iVx(0),iVy(0),iRadius(aRadius)
{
setPos(aPosX,aPosY);
}
QRectF BallItem::boundingRect() const
{
qreal adjust = 0.5;
QRectF rectf = QRectF(-iRadius-adjust,-iRadius-adjust,iRadius*2 + adjust
,iRadius*2 + adjust);
return rectf;
}
void BallItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
painter->save();
painter->setBrush(Qt::red);
painter->drawEllipse(QPointF(0,0),iRadius,iRadius);
painter->restore();
}
To copy to clipboard, switch view to plain text mode
Here I initialise QGraphicsView(iGameView) and QGraphicsScene(iGameScene)
iGameView
->setRenderHint
(QPainter::Antialiasing);
iGameView
->setViewportUpdateMode
(QGraphicsView::BoundingRectViewportUpdate);
iGameView->setBackgroundBrush(Qt::green);
iGameView->setAttribute(Qt::WA_LockPortraitOrientation, true);
iGameView->setScene(iGameScene);
iGameView->setRenderHint(QPainter::Antialiasing);
iGameView->setCacheMode(QGraphicsView::CacheBackground);
iGameView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
iGameView->setBackgroundBrush(Qt::green);
iGameView->setAttribute(Qt::WA_LockPortraitOrientation, true);
iGameScene->setItemIndexMethod(QGraphicsScene::NoIndex);
iGameView->setScene(iGameScene);
To copy to clipboard, switch view to plain text mode
I make position updates in advance method.
void BallItem::advance(int phase)
{
if (!phase)
return;
bool flipX = false;
bool flipY = false;
qreal newX = x();
qreal newY = y();
if (newX + iRadius + iVx > scene()->width())
{
newX = scene()->width() - iRadius;
flipX = true;
}
else if (newX - iRadius +iVx < 0)
{
newX = iRadius;
flipX = true;
}
else
newX+=iVx;
if (newY + iRadius + iVy > scene()->height())
{
newY = scene()->height() - iRadius;
flipY = true;
}
else if (newY - iRadius + iVy < 0)
{
newY = iRadius;
flipY = true;
}
else
newY+=iVy;
setPos(newX,newY);
if (flipX)
iVx = -iVx;
if (flipY)
iVy = -iVy;
}
void BallItem::advance(int phase)
{
if (!phase)
return;
bool flipX = false;
bool flipY = false;
qreal newX = x();
qreal newY = y();
if (newX + iRadius + iVx > scene()->width())
{
newX = scene()->width() - iRadius;
flipX = true;
}
else if (newX - iRadius +iVx < 0)
{
newX = iRadius;
flipX = true;
}
else
newX+=iVx;
if (newY + iRadius + iVy > scene()->height())
{
newY = scene()->height() - iRadius;
flipY = true;
}
else if (newY - iRadius + iVy < 0)
{
newY = iRadius;
flipY = true;
}
else
newY+=iVy;
setPos(newX,newY);
if (flipX)
iVx = -iVx;
if (flipY)
iVy = -iVy;
}
To copy to clipboard, switch view to plain text mode
I am sorry for attaching so much source code but I know that there are some issues about mapping from item coordinate system to scene coordinate system and maybe I do something wrong in presented source code. Something what will be caught by more experienced eye.
Bookmarks