Search code examples
qtqgraphicsviewqgraphicsitemqgraphicsscene

How to put several QImage in a QGraphicsView?


I have a scene where several items are added. The problem is that when the items are displayed, they are overlapping. Is there any way to indicate in the QGraphicsView or QGraphicsScene the position where each item should appear?


Solution

  • Yes, you have to use QGraphicsItem::setPos() method. I suppose you added a QGraphicsPixmapItem, so it could look like :

    QGraphicsScene *scene = ... ; // your scene
    QImage image = ... ; // the QImage you want to add to the scene
    QPixmap pixmap = QPixmap::fromImage(image) ;
    
    // add image item to the scene
    QGraphicsPixmapItem * imageItem = scene->addPixmap(pixmap) ;
    
    // modify item's position in scene coordinates
    QPointF imagePos = ... ; // whatever scene pos you want
    imageItem->setPos(imagePos) ;