Results 1 to 2 of 2

Thread: How to improve memory-consumption when drawing polygons, (poly-)lines and points

  1. #1
    Join Date
    Nov 2013
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default How to improve memory-consumption when drawing polygons, (poly-)lines and points

    I'm currently struggling with populating a qgraphicsscene with many (> 1Million) objects (Polygons, Lines, Points). What I've observed is, that when creating randomly 100000 Polygons, I'm already ending up withe 130MB memory consumption. (The simple example below is based on a default Qt-Project using the View.cpp of the chip-demo example)
    Qt Code:
    1. QtGrafikTestBasic::QtGrafikTestBasic(QWidget *parent): QMainWindow(parent)
    2. {
    3. ui.setupUi(this);
    4. QSplitter *h1Splitter = new QSplitter;
    5. QSplitter *vSplitter = new QSplitter;
    6. vSplitter->addWidget(h1Splitter);
    7.  
    8. View* view = new View("asdf");
    9. h1Splitter->addWidget(view);
    10.  
    11. QHBoxLayout *layout = new QHBoxLayout;
    12. layout->addWidget(vSplitter);
    13. setLayout(layout);
    14. setCentralWidget(view);
    15.  
    16. QBrush *brush = new QBrush();
    17. brush->setColor(Qt::blue);
    18. brush->setStyle(Qt::SolidPattern);
    19. QPen *pen = new QPen();
    20. pen->setWidth(0);
    21.  
    22. srand ( time(NULL) );
    23. int m_PolyWidth = 10;
    24.  
    25. for (int i = 0 ; i < 100000; i++)
    26. {
    27. double lBaseX = rand() % ((int)floor(width()) - m_PolyWidth);
    28. double lBaseY = rand() % ((int)floor(height()) - m_PolyWidth);
    29.  
    30. QPolygonF polygon;
    31. polygon << QPointF(lBaseX, lBaseY);
    32. polygon << QPointF(lBaseX + m_PolyWidth, lBaseY);
    33. polygon << QPointF(lBaseX + m_PolyWidth, lBaseY + m_PolyWidth);
    34. polygon << QPointF(lBaseX, lBaseY + m_PolyWidth);
    35.  
    36. scene->addPolygon(polygon, *pen, *brush);
    37. }
    38. view->view()->setScene(scene);
    39. }
    To copy to clipboard, switch view to plain text mode 

    So what am I doint wrong here / where can I improve? I've read some posts of creating an own class like the chip-example, so I simply used the chip example but also there I've encountered the problem that as soon as I change the part which uniformly distributes the chips from
    Qt Code:
    1. item->setPos(QPointF(i, j));
    To copy to clipboard, switch view to plain text mode 
    to a random distribution
    Qt Code:
    1. item->setPos(QPointF(lBaseX, lBaseY));
    To copy to clipboard, switch view to plain text mode 
    , the memory consumption explodes also here...

    So, what is the most performant and least memory-consuming way of drawing polygons, (poly-)lines and points in Qt?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to improve memory-consumption when drawing polygons, (poly-)lines and points

    I guess the most memory efficient way is to draw yourself using the QPainter API.
    This way you can work with the data you already have and don't need to copy it.

    Alternatively create your own graphics item that does not require a copy of the data but has access to your already existing data.
    That will still require at least one pointer for the item, one to point to the global data and some index into the data.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 7th March 2013, 06:14
  2. QString memory consumption
    By sicker in forum Newbie
    Replies: 1
    Last Post: 4th November 2011, 16:23
  3. QGraphicsScene - memory consumption
    By mateuszzz88 in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 29th November 2010, 20:39
  4. drawing concave polygons
    By bhogasena in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2009, 07:59
  5. QTableView memory consumption
    By LordQt in forum Qt Programming
    Replies: 7
    Last Post: 9th December 2008, 16:51

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.