I am developing a ground control station for a small drone, where I am trying to add a function to load waypoints from a file.
Each waypoint is a QGraphicsItem
on the QGraphicsScene
.
However, when there are more than 100 points in the file, the creation takes more than 2 seconds...
Is there any way to do this faster?
The 40K Chips example shows off the population of a scene with a large number of elements.
http://doc.qt.io/qt-5/qtwidgets-graphicsview-chip-mainwindow-cpp.html
void MainWindow::populateScene()
{
scene = new QGraphicsScene;
QImage image(":/qt4logo.png");
// Populate scene
int xx = 0;
int nitems = 0;
for (int i = -11000; i < 11000; i += 110) {
++xx;
int yy = 0;
for (int j = -7000; j < 7000; j += 70) {
++yy;
qreal x = (i + 11000) / 22000.0;
qreal y = (j + 7000) / 14000.0;
QColor color(image.pixel(int(image.width() * x), int(image.height() * y)));
QGraphicsItem *item = new Chip(color, xx, yy);
item->setPos(QPointF(i, j));
scene->addItem(item);
++nitems;
}
}
}
If you have a lot of initialization code going on in here, then it may take a long time. You may want to look into subclassing your QGraphicsItem
if it is slow loading. The level of detail argument in the custom item here (see chip.cpp) is pretty cool how it handles everything.
Hope that helps.