0

I need to manage a scene with a high number of static item, but some item will be the same but at 10k+ different coordinates. For example, there's one circle, but drawed 10k times in the scene.

The only solution I found was to use 2 scenes for the same viewport, which is obviously not the solution I need since I need many simple objects. It it possible to do this using QGraphicsScene/QgraphicView ?

Krapow
  • 611
  • 6
  • 26
  • I suggest looking at Qt's [40000 chip demo](http://doc.qt.io/qt-5/qtwidgets-graphicsview-chip-example.html) as an example to follow – TheDarkKnight Oct 29 '15 at 10:33
  • In this example, there's 40000 chips created. My question was, is it possible to instanciate one object then place it 40000 times. This would take less RAM I think. – Krapow Oct 29 '15 at 10:58

1 Answers1

1

is it possible to instanciate one object then place it 40000 times

No, Qt doesn't work like this. That's like asking if you can be in two places at the same time.

Qt is designed to be efficient, so that if you have multiple objects, for example a QGraphicsPixmapItem with the same resource image, it can use the same image for all the items. However, an item can only be at one location in the scene at any one time.

So, in the case of a circle, drawn 10000 times, you can create 10000 graphics items, all using the same QPixmap resource, which is the circle. However, you still need to create 10000 items as each item must store its coordinates and orientation somewhere; that being the QGraphicsItem.

Let's say we have instantiated a QPixmap item with the circle:

QPixmap* pCircle = new QPixmap(":/images/circle"); // circle from the resource system

We can now create 10000 items, at different locations, each using that circle:

for(int i = 0; i<10000; ++i)
{
    QGraphicsPixmapItem* pItem = new QGraphicsPixmapItem(*pCircle);
    // set its position and add it to the scene
    pItem->setPos(x, y);
    m_pScene->addItem(pItem);
}
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • I have a question about you code, if pcirle change, will the pItem change too? – Krapow Oct 29 '15 at 14:51
  • No, it won't. If you read about Qt's [Implicit Sharing](http://doc.qt.io/qt-5/implicit-sharing.html), you'll gain a better understanding of what's happening. – TheDarkKnight Oct 29 '15 at 15:00
  • This is horribly slow down or hang the UI. If you keep doing this in ms intervals – taimoor1990 Jul 27 '20 at 04:39
  • @taimoor1990, please explain the "ms intervals", as there is no timing in the displayed source. – TheDarkKnight Jul 27 '20 at 17:23
  • Let's say you initially added 10k graphicsitems and painted them on the scene as according to your code. Now the thing is, if you are acquiring new positions after lets say after every 500ms. These reinitializations will take too much UI thread time and hence it will block Ui. This is my understanding of the problem – taimoor1990 Jul 27 '20 at 18:27
  • I am still looking for some viable solution of this problem. When the problem starts, in the constructor I can initialize and declare 100k items but when I need to change the positions of these items I have to reiterate to these 100k items again and again. Now lets say you are getting new info after 200ms and items updating will take time 300ms, this is the issue. – taimoor1990 Jul 27 '20 at 18:32
  • Currently I have subclassed qgraphicsitem, results are comparatively better but still I am not satisfied – taimoor1990 Jul 27 '20 at 18:34
  • @taimoor1990 Perhaps [this will help](https://stackoverflow.com/questions/18397603/how-to-move-around-1000-items-in-a-qgraphicsscene-without-blocking-the-ui/18400665#18400665) – TheDarkKnight Aug 04 '20 at 14:31