Results 1 to 6 of 6

Thread: QGraphicsView and many QGraphicsPixmapItem items - memory managment

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsView and many QGraphicsPixmapItem items - memory managment

    Hello!

    I'm working on project focused on map similar to Google Maps. The goal is to display whole map in QGraphicsView widget. To do this I created a QGraphicsScene built of many QGraphicsPixmapItem items. Each item stands for one map tile (A small PNG graphic). Because map is quite large (it's square of about 512x512 tiles and more) I'm trying to load tiles on demand (when they're visible in QGraphicsView widget's viewport area).

    Some parts of code:

    I create all tiles (QGraphicsPixmapItem items) at the beginning and set them with "empty" QPixmap:
    Qt Code:
    1. MapTile::MapTile(int zoomLevel, ...)
    2. {
    3. bool returnStatus (false);
    4.  
    5. returnStatus = QFile::exists(this->tilePath);
    6. if(!returnStatus)
    7. {
    8. qWarning("Faield to locate tile PNG!");
    9. return;
    10. }
    11.  
    12. this->setPixmap(QPixmap (256, 256));
    13. this->setPos (this->pixelsPosition.getX(), this->pixelsPosition.getY());
    14. }
    To copy to clipboard, switch view to plain text mode 

    Then I add each tile to scene.

    I created class based on QGraphicsView named MapView and overloaded resizeEvent() and scrollContentsBy() methods. Code sample below shows only scrollContentsBy(), resizeEvent() is analogous.

    First thing, I map top-left and bottom-right corners of QGraphicsView object into scene coordinates (pointa, pointb). Later in first loop I unload (load "empty" pixmap) all items assigned to scene (I know, it's stupid to unload all tiles, but I will make this part of code smarter later). In next two nested loops I pick only items currently visible in QGraphicsView object and load pixmaps into them.
    Qt Code:
    1. void MapView::scrollContentsBy ( int dx, int dy )
    2. {
    3. this->QGraphicsView::scrollContentsBy(dx,dy);
    4.  
    5. MapScene* currentScene = this->zoomLevelScenes [this->currentZoomLevel];
    6. QPointF pointa (this->mapToScene(0, 0)); // top-left
    7. QPointF pointb (this->mapToScene(this->height(), this->width())); //bottom-right
    8. QList<QGraphicsItem*> list = currentScene->items();
    9.  
    10. foreach(QGraphicsItem* item, list)
    11. {
    12. ((MapTile*)item)->loadPixmap(false);
    13. }
    14.  
    15. for (qreal x = pointa.x() - 300; x < pointb.x() + 300; x += 255)
    16. {
    17. for (qreal y = pointa.y() - 300; y < pointb.y() + 300; y += 255)
    18. {
    19. MapTile* tile ((MapTile*) currentScene->itemAt(x,y));
    20. if (tile!=0)
    21. tile->loadPixmap(true);
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    My MapTile::loadPixmap() method:
    Qt Code:
    1. void MapTile::loadPixmap(bool status)
    2. {
    3. QPixmap pixmap (256, 256);
    4. bool returnStatus (false);
    5.  
    6. if (status)
    7. {
    8. returnStatus = pixmap.load(this->tilePath);
    9. if(!returnStatus)
    10. {
    11. qWarning("Faield to load tile PNG!");
    12. return;
    13. }
    14. }
    15.  
    16. this->pixmapLoaded = status;
    17. this->setPixmap(pixmap);
    18. }
    To copy to clipboard, switch view to plain text mode 

    Code seems to work fine, because folder with tiles has about 500 MB and my program after start use only about ~25 MB. I also see correct map in window (there are no blank tiles displayed). The problems is that when I scroll map or resize window program is slowly, but constantly allocating more and more memory (on single resize event it allocates another 0.2 - 0.4 MB of RAM). After some time of working with program it becomes serious.

    Any idea what's wrong? I assume, that I'm doing something wrong, right? Or maybe I'm not aware of some data chaching method used in Qt? I worked a lot with GTK+ and i'm pretty new to Wt...
    Last edited by goofacz; 17th November 2011 at 16:03.

Similar Threads

  1. QGraphicsView + steady Items
    By medved6 in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2010, 19:20
  2. Changing memory allocation managment ( QT 4.4.3 )
    By Link130 in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 20th October 2009, 09:44
  3. QGraphicsView properties and performance/memory
    By vkokk in forum Qt Programming
    Replies: 4
    Last Post: 16th July 2009, 18:12
  4. Memory Managment Question
    By tntcoda in forum Qt Programming
    Replies: 4
    Last Post: 25th June 2008, 16:34
  5. QGraphicsView overlapping items
    By juliarg in forum Newbie
    Replies: 1
    Last Post: 5th April 2007, 09:35

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.