1
votes

I am trying to use the Qt Graphics Framework to draw a graph with a large number of nodes (2000+). Using the QGraphicsView and the QGraphicsScene I can easily draw large number of ellipses with the addEllipse() method, but changing the color of the items with setBrush() is way too slow. I have also tried using QGraphicsPixmapItem and then just switch the pixmap to change color but this also does not work. Can you give me any advice how to accomplish this? Is using OpenGL the only way I can get better performance?

Thanks a lot.

Edit: I do not have the brush example. Will post it later. The code just swap brushes of the ellipse nothing more,

Edit: Pixmap example (Code is in python with PyQt):

def startTest(self):
    self.n = 1000
    self.c = 1
    self.scene = QtGui.QGraphicsScene()
    self.scene.setSceneRect(0,0,500,500)
    self.graphicsView.setScene(self.scene)
    self.redPix = QtGui.QPixmap(5, 5)
    self.redPix.fill(QtGui.QColor(255,0,0))
    self.blackPix = QtGui.QPixmap(5, 5)
    self.blackPix.fill(QtGui.QColor(0,0,0))

    for i in range(0,self.n):
        temp = QtGui.QGraphicsPixmapItem(self.redPix)
        temp.setPos(random.uniform(10, 490),random.uniform(10, 490))
        self.scene.addItem(temp)

def updateNodes(self):

        if self.c:
            for i in self.scene.items():
                i.setPixmap(self.blackPix)
            self.c = 0
        else:
            for i in self.scene.items():
                i.setPixmap(self.redPix)
            self.c = 1
2
can you post some code?? - UmNyobe

2 Answers

1
votes

Well, may be it will be not really popular answer.. but from my experience I dont think that QGraphicsScene/View way is applicable on something with 1000k+ objects. It's great framework which has a lot of nice features but the way it's designed makes it really slow for large amount of objects.

An amount of code you have to do yourself (if you want to get decent performance with so many objects) /by this I mean clipping/filtering/possibly generalisation/ is really far too much in compare if you make own task dependent rendering core.

I use Qt a lot for many years and tried to use View/Scene for serious graphics components (different map formats views SDK's, 2D graphics design views) and every time I ended up with self made rendering engine.

Main reason is that Qt tries to provide you with very generic and it's very very good way of handling graphics objects. They have very well made transformation matrixes concept, double based coordinates, common styling approaches, but price is that they are missing all most common principles of optimisation for heavy graphic views.

0
votes

Have you tried specifying using the brush parameter of the addEllipse() method?

QBrush blueBrush = QBrush(Qt::blue);
QPen noPen;
scene.addEllipse(x, y, w, h, noPen, blueBrush);