Results 1 to 5 of 5

Thread: QGraphicsItem flickering

  1. #1
    Join Date
    Feb 2011
    Location
    Poland
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default QGraphicsItem flickering

    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:

    Qt Code:
    1. BallItem::BallItem(qreal aPosX, qreal aPosY, qreal aRadius,QGraphicsItem *aParent) :
    2. QGraphicsItem(aParent),
    3. iVx(0),iVy(0),iRadius(aRadius)
    4. {
    5. setPos(aPosX,aPosY);
    6. }
    7.  
    8.  
    9. QRectF BallItem::boundingRect() const
    10. {
    11. qreal adjust = 0.5;
    12. QRectF rectf = QRectF(-iRadius-adjust,-iRadius-adjust,iRadius*2 + adjust
    13. ,iRadius*2 + adjust);
    14. return rectf;
    15. }
    16.  
    17.  
    18. void BallItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    19. QWidget *widget)
    20. {
    21. painter->save();
    22.  
    23. painter->setBrush(Qt::red);
    24. painter->drawEllipse(QPointF(0,0),iRadius,iRadius);
    25.  
    26. painter->restore();
    27. }
    To copy to clipboard, switch view to plain text mode 

    Here I initialise QGraphicsView(iGameView) and QGraphicsScene(iGameScene)

    Qt Code:
    1. iGameView->setRenderHint(QPainter::Antialiasing);
    2. iGameView->setCacheMode(QGraphicsView::CacheBackground);
    3. iGameView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    4. iGameView->setBackgroundBrush(Qt::green);
    5. iGameView->setAttribute(Qt::WA_LockPortraitOrientation, true);
    6.  
    7. iGameScene->setItemIndexMethod(QGraphicsScene::NoIndex);
    8.  
    9. iGameView->setScene(iGameScene);
    To copy to clipboard, switch view to plain text mode 

    I make position updates in advance method.
    Qt Code:
    1. void BallItem::advance(int phase)
    2. {
    3. if (!phase)
    4. return;
    5.  
    6. bool flipX = false;
    7. bool flipY = false;
    8.  
    9. qreal newX = x();
    10. qreal newY = y();
    11.  
    12. if (newX + iRadius + iVx > scene()->width())
    13. {
    14. newX = scene()->width() - iRadius;
    15. flipX = true;
    16. }
    17. else if (newX - iRadius +iVx < 0)
    18. {
    19. newX = iRadius;
    20. flipX = true;
    21. }
    22. else
    23. newX+=iVx;
    24.  
    25. if (newY + iRadius + iVy > scene()->height())
    26. {
    27. newY = scene()->height() - iRadius;
    28. flipY = true;
    29. }
    30. else if (newY - iRadius + iVy < 0)
    31. {
    32. newY = iRadius;
    33. flipY = true;
    34. }
    35. else
    36. newY+=iVy;
    37.  
    38. setPos(newX,newY);
    39.  
    40. if (flipX)
    41. iVx = -iVx;
    42.  
    43. if (flipY)
    44. iVy = -iVy;
    45.  
    46. }
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem flickering

    You are drawing outside your boundingRect(). Calling setPos() in the item constructor doesn't make much sense either.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2011
    Location
    Poland
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: QGraphicsItem flickering

    Do you mean adding adjust*2 when I specify rectf width and height?

    Qt Code:
    1. QRectF BallItem::boundingRect() const
    2. {
    3. qreal adjust = 0.5;
    4. QRectF rectf = QRectF(-iRadius-adjust, -iRadius-adjust, iRadius*2 + adjust*2
    5. , iRadius*2 + adjust*2);
    6.  
    7. return rectf;
    8. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem flickering

    Simplify the code (QRectF has a bunch of nice methods). And what's the purpose of calling setPos() in the constructor?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2011
    Location
    Poland
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: QGraphicsItem flickering

    I am going to rewrite it so it will look better.

    If it comes to constructor, hmm well it is a remaining from my try-outs, setPos() is going to be removed. Now I know it is not the best way to do so, but when I started with QGraphics... stuff I didn't know that.

Similar Threads

  1. Qt flickering animation
    By tomrider in forum Qt Programming
    Replies: 5
    Last Post: 5th May 2011, 14:31
  2. QGraphicsItem flickering problems
    By aladagemre in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2010, 16:48
  3. Flickering problem
    By yagabey in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2010, 12:25
  4. Flickering
    By Pembar in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2009, 19:21
  5. QtOgreFramework flickering with qt 4.5
    By Angelo Moriconi in forum Qt Programming
    Replies: 2
    Last Post: 6th April 2009, 10:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.