The reason it doesn't work is because you are trying to use two different QGraphicsScene
s for your QGraphicsView
. QGraphicsView
can only have one scene.
What you should do, is create only one QGraphicsScene
and add your pixmaps there.
QGraphicsScene *scene = new QGraphicsScene(this);
ui->graphicsScene->setScene(scene);
QPixmap pix1(n1);
QGraphicsPixmapItem *pixmapItem1 = scene->addPixmap(pix1);
QPixmap pix2(n2);
QGraphicsPixmapItem *pixmapItem2 = scene->addPixmap(pix2);
pixmapItem2->moveBy(0, pix1.height());
Also your QGraphicsOpacityEffect
object is only valid inside the scope you created it in. One way to solve this problem is to allocate it with new
.
QGraphicsOpacityEffect *opacity1 = new QGraphicsOpacityEffect;
QGraphicsOpacityEffect *opacity2 = new QGraphicsOpacityEffect;
opacity1->setOpacity(0.5);
opacity2->setOpacity(0.2);
pixmapItem1->setGraphicsEffect(opacity1);
pixmapItem2->setGraphicsEffect(opacity2);