Results 1 to 17 of 17

Thread: graphics view slow

Threaded View

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

    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.

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
  •  
Qt is a trademark of The Qt Company.