It's my first post here and I would like to say Hello Everyone on stackoverflow :)
OK, welcome already have been and now I specify the problem which I've got. I have one QGraphicsView widget and I want to show two images with some opacity in it, but my code doesn't work and I don't know what is the reason :/
QGraphicsScene *scenaWynikowa = new QGraphicsScene(ui->graphicsViewWynik);
ui->graphicsViewWynik->setScene(scenaWynikowa);
ui->graphicsViewWynik->fitInView(scenaWynikowa->itemsBoundingRect(), Qt::KeepAspectRatio);
//wyświetlenie zdjęcia nr 1
QImage obraz1(s1);
obraz1.scaled(QSize(541,541), Qt::IgnoreAspectRatio, Qt::FastTransformation);
update();
resize(541, 541);
QPixmap mapaPikseli1(n1);
QGraphicsPixmapItem *pixmapItem1 = scenaWynikowa->addPixmap(mapaPikseli1);
QGraphicsOpacityEffect poziomPrzezroczystosci1;
poziomPrzezroczystosci1.setOpacity(0.5);
pixmapItem1->setGraphicsEffect(&poziomPrzezroczystosci1);
//wyświetlenie zdjęcia nr 2
QImage obraz2(s2);
obraz2.scaled(QSize(541,541), Qt::IgnoreAspectRatio, Qt::FastTransformation);
update();
resize(541, 541);
QPixmap mapaPikseli2(n2);
QGraphicsPixmapItem *pixmapItem2 = scenaWynikowa->addPixmap(mapaPikseli2);
QGraphicsOpacityEffect poziomPrzezroczystosci2;
poziomPrzezroczystosci2.setOpacity(0.5);
pixmapItem2->setGraphicsEffect(&poziomPrzezroczystosci2);
pixmapItem2->moveBy(0, 0);
ui->graphicsViewWynik->show();
Sorry for not English Variable's names but it's more convinient for me. If you want I can explain what and why variable has that name :) Maybe someone finds a mistake in this code and explain to me where's the problem with my code and how to fix it?
edit: It's my new code. When I move position of pix2 on QGraphicsView I can see two images (pix2 under pix1)and it works fine, but images should have opacity level to make a diffusion effect. How should I do it?
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);