Results 1 to 17 of 17

Thread: graphics view slow

  1. #1
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation graphics view slow

    hi guys

    I have this problem that i try to resolve it for few days, but no progress. i don't even know is that possible to resolve.

    i am writting a Tank game. i have a main timer move all the items in the scene. QObject::startTimer(20);
    I have a tank move in the scene, and i use graphicsView.ensureVisible to keep it around center of viewport.

    so the scene is big (5000,5000) viewport is about 1000,1000, viewport will move when tank moves out of center rect.

    i set tank move 10 pixels very timer trigger.

    the problem is
    when tank moves, it become fuzzy.
    when i change it to 3 pixels, it is not fuzzy, but it was very slow, i need to make it move fast. It is tank, not the ant.
    So I change the timer to startTimer(3). It works good if the Tank move inside of center rect(viewport is not move).
    1. but when viewport moves, tank is slow down!!!!!!!

    2. when i resize the viewport, make it smaller, the speed of tank is move more fast than it in bigger viewport.

    i think it may has something to do with the speed of paint. the bigger view port takes longer to paint, so 3 millisecond is not enough for it to paint.

    i don't know. any ideas? thank advance

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: graphics view slow

    Have you tried playing with Graphics view caching strategies?


    Using a proper combination of these could result in a dramatic increase of performance (there's no warranty though as the efficiency of these optimizations depends on the scene).

    There is probably room for improvement in your design as well but the description you gave is not clear enough for me to give you any hints on this. The only thing I can tell without more konwledge of your code is that a proper use of item transformations could help.

    If you need smotth animations you may consider exploring Qt labs and giving a try to Qt kinetic.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: graphics view slow

    graphicsView.setBackgroundBrush(QBrush(backGroundC olor));
    graphicsView.setCacheMode(QGraphicsView::CacheBack ground);
    graphicsView.setRenderHint(QPainter::Antialiasing) ;
    graphicsView.setViewportUpdateMode(QGraphicsView:: SmartViewportUpdate);//QGraphicsView::FullViewportUpdate);////BoundingRectViewportUpdate);
    graphicsView.setDragMode(QGraphicsView::NoDrag);

    scene.setItemIndexMethod(QGraphicsScene::NoIndex);

    this is my current settings

    this game is simple, tank move controlled by keyboard.
    there are some rectangles to be shoot at.
    the tank is the picture.
    there are four pictures for up down left and right, they are preloaded.
    "
    tankUp = new QImage(":/CannonBattle/Resources/tank up.jpg");
    tankDown= new QImage(":/CannonBattle/Resources/tank down.jpg");
    tankRight =new QImage(":/CannonBattle/Resources/tank right.jpg");
    tankLeft = new QImage(":/CannonBattle/Resources/tank left.jpg");
    "
    so i did not use any rotation and transformations.

    all the paint and collision check happened in one timer.

    the way i make the tank move is to setpos(), then call centeron();

    is transformation fast than setpos?

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

    Default Re: graphics view slow

    First get rid of "ensure visible" functionality as the view has to recalculate the frustum all the time which is pointless because at each moment you know exactly if your tank is visible on the viewport or not.

    Second of all firing a timer every 3 miliseconds doesn't make much sense. Having 333 frames per second differs from having 25 frames per second only in one thing - that you strain your machine almost 15 times more. If you set the period of the timer to 40ms (which gives you 25fps) your performance will improve (you can compensate for the distance to have a similar impression of "speed").

    Third of all think if you really need GraphicsView for this use case, maybe it only slows you down.

    Fourth of all don't allocate QImage objects on the heap, it doesn't make sense to do so.
    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
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: graphics view slow

    Hi man, thanks very much for your reply. I still have some questions.

    1. "First get rid of "ensure visible" functionality ", I need it to keep it in viewport, sence is much big than viewport. any other way?

    2."If you set the period of the timer to 40ms (which gives you 25fps) your performance will improve (you can compensate for the distance to have a similar impression of "speed").
    "
    I tried, in order to get same speed i have to move tank 15 pixels very timer which makes tank move serious blurry

    3 ." if you really need GraphicsView for this use case, maybe it only slows you down."
    any they other way?

    4. "don't allocate QImage objects on the heap, it doesn't make sense to do so. "
    what should i use? bitmap?

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

    Default Re: graphics view slow

    Quote Originally Posted by dognzhe View Post
    1. "First get rid of "ensure visible" functionality ", I need it to keep it in viewport, sence is much big than viewport. any other way?
    Scroll the viewport yourself. QGraphicsView is derived from QAbstractScrollArea.

    2."If you set the period of the timer to 40ms (which gives you 25fps) your performance will improve (you can compensate for the distance to have a similar impression of "speed").
    "
    I tried, in order to get same speed i have to move tank 15 pixels very timer which makes tank move serious blurry
    So find something in between.

    3 ." if you really need GraphicsView for this use case, maybe it only slows you down."
    any they other way?
    Yes, QWidget

    4. "don't allocate QImage objects on the heap, it doesn't make sense to do so. "
    what should i use? bitmap?
    No. You should learn reading the docs and at least a bit of C++. Now go google for "heap" and read QImage docs.
    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.


  7. #7
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: graphics view slow

    this is funny reply.



    find something in between. will not achieve the desired speed!

    scroll by user is nonsense, have you play any game with scroll bar? unbelievable

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

    Default Re: graphics view slow

    Quote Originally Posted by dognzhe View Post
    scroll by user is nonsense, have you play any game with scroll bar? unbelievable
    Who said anything about the user? I meant you as a programmer.

    The fact that you don't understand what I said doesn't mean it is funny. If you design your application correctly you will achieve everything you want without problems. If you misdesign, you'll be running into problems on every step like it is happening now.
    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.


  9. #9
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Re: graphics view slow

    my logic is very simple, there is timer, when time out do all the drawing


    any one can help??????

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

    Default Re: graphics view slow

    You're not listening. The problem is very unlikely to be drawing. If you have doubts about it, take a profiler and find the bottleneck.
    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.


  11. #11
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Re: graphics view slow

    Qt Code:
    1. #include "Tank.h"
    2.  
    3. Tank::Tank(QObject *parent,QGraphicsScene *scene)
    4. : QObject(parent), QGraphicsItem(0,scene)
    5. {
    6. tankDirection=dRight;
    7. tankUp = new QPixmap(":/CannonBattle/Resources/tank up.jpg");
    8. tankDown= new QPixmap(":/CannonBattle/Resources/tank down.jpg");
    9. tankRight =new QPixmap(":/CannonBattle/Resources/tank right.jpg");
    10. tankLeft = new QPixmap(":/CannonBattle/Resources/tank left.jpg");
    11. if(tankUp->isNull() && tankDown->isNull() && tankRight->isNull()){
    12. emit error(QString("tank image load fails"));
    13. }
    14. /*
    15.  
    16.   label.setText("dongzhe adfasd fasdfsdafsadfasdfasd");
    17.   //label.setMovie(movie);
    18.  mo=this->scene()->addWidget(&label);
    19.  mo->setPos(50,50);
    20.  */
    21.  
    22. this->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
    23.  
    24. }
    25.  
    26. Tank::~Tank()
    27. {
    28.  
    29. }
    30.  
    31. QPixmap* Tank::getImage() const{
    32.  
    33. if(tankDirection==dRight){
    34. return tankRight;
    35. }else if(tankDirection== dLeft){
    36. return tankLeft;
    37. }else if(tankDirection== dUp){
    38. return tankUp;
    39. }else{
    40. return tankDown;
    41. }
    42.  
    43. }
    44.  
    45. QRectF Tank::boundingRect() const{
    46.  
    47. //increase the boudingRect to get rid of marks
    48. return QRectF(getImage()->rect().x(),getImage()->rect().y(),getImage()->rect().width(),getImage()->rect().height()+2);
    49. //return getImage()->rect();
    50. }
    51.  
    52. void Tank::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    53.  
    54. painter->drawPixmap(QPoint(0,0),*getImage());
    55. //painter->drawImage(QPoint(0,0),QImage(":/CannonBattle/Resources/explose.gif"));
    56.  
    57.  
    58.  
    59. }
    60.  
    61.  
    62. int Tank::type() const {
    63.  
    64. return TANK_TYPE;
    65. }
    66.  
    67. [/QTCLASS]
    68.  
    69.  
    70. here is move tank
    71.  
    72. [QTCLASS]
    73. void CannonField::tankMove(){
    74. #if _DEBUG
    75. int a;
    76. int b;
    77. timeTemp.start();
    78. //timeTemp =QTime::currentTime();
    79.  
    80. #endif
    81.  
    82. QPointF oldPoint(mainTank->pos());
    83. tankCurrentY = mainTank->pos().y();
    84. tankCurrentX= mainTank->pos().x();
    85. if(mainTank->tankDirection==mainTank->dUp){
    86. mainTank->setPos(tankCurrentX, tankCurrentY-tankSpeed);
    87. }
    88.  
    89. if(mainTank->tankDirection==mainTank->dDown){
    90. mainTank->setPos(tankCurrentX, tankCurrentY+tankSpeed);
    91. }
    92.  
    93. if(mainTank->tankDirection==mainTank->dLeft){
    94. mainTank->setPos(tankCurrentX-tankSpeed,tankCurrentY);
    95. }
    96.  
    97. if(mainTank->tankDirection==mainTank->dRight){
    98. mainTank->setPos(tankCurrentX+tankSpeed,tankCurrentY);
    99. }
    100.  
    101. tankCurrentY = mainTank->pos().y();
    102. tankCurrentX= mainTank->pos().x();
    103.  
    104.  
    105. if (tankCurrentX <0 || tankCurrentY<0 || tankCurrentX > (scene.width()-mainTank->boundingRect().width()) || tankCurrentY > (scene.height()-mainTank->boundingRect().height())) {
    106. mainTank->setPos(oldPoint);
    107. }
    108.  
    109. QList<QGraphicsItem *> collisions = mainTank->collidingItems(Qt::IntersectsItemBoundingRect);
    110. if(!collisions.isEmpty()){
    111.  
    112. QListIterator<QGraphicsItem *> it(collisions);
    113. while(it.hasNext()){
    114. temp= it.next();
    115. if(temp->type()==TARGET_TYPE){
    116. mainTank->setPos(oldPoint);
    117. }
    118. }
    119.  
    120. }
    121.  
    122.  
    123. //graphicsView.centerOn(mainTank);
    124. graphicsView.ensureVisible(mainTank,graphicsView.rect().width()/3,graphicsView.rect().height()/3);
    125. #if _DEBUG
    126. //for(int i=0;i<1000000;i++)
    127. //{i++;}
    128. //b=QTime::currentTime().msecsTo(timeTemp);
    129. a= timeTemp.elapsed();
    130. qDebug("%d\n",a);
    131. #endif
    132. }
    To copy to clipboard, switch view to plain text mode 


    I don;'t know what to do ...
    Last edited by wysota; 8th May 2009 at 06:39.

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

    Default Re: graphics view slow

    Please take a profiler and find the bottle neck in your application. Now you're just shooting blind.
    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.


  13. #13
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: graphics view slow

    Quote Originally Posted by wysota View Post
    Please take a profiler and find the bottle neck in your application. Now you're just shooting blind.
    where can i get profiler? is that free? can you give me a link????

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

    Default Re: graphics view slow

    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.


  15. #15
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: graphics view slow

    man, i can 't find the free one... i am using VS2005 with qt.

    do you know any free one for downlaod?

  16. #16
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

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

    Default Re: graphics view slow

    Quote Originally Posted by dognzhe View Post
    man, i can 't find the free one... i am using VS2005 with qt.
    Consider switching to a Free compiler then.
    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.


Similar Threads

  1. Graphics View Panning ,zooming
    By linuxdev in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2008, 07:17
  2. Replies: 4
    Last Post: 5th August 2008, 19:55
  3. Graphics View Event Propagation
    By pherthyl in forum Qt Programming
    Replies: 10
    Last Post: 3rd July 2008, 10:39
  4. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  5. Adding Rectangular overlay on graphics view
    By forrestfsu in forum Qt Programming
    Replies: 10
    Last Post: 21st November 2006, 19:42

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.