Domanda

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?

È stato utile?

Soluzione

The reason it doesn't work is because you are trying to use two different QGraphicsScenes 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);

Altri suggerimenti

Ok. Thanks @thuga for your help. The problem has been solved. What was wrong? I was setting Opacity twice to two another variables but it was huge mistake. In QGraphicsView we can only declare OpacityEffect variable once, and assign it to multiple variables - just like QGraphicsScene.

Latest version of code (it works fine):

QGraphicsScene *scenaWynikowa = new QGraphicsScene(ui->graphicsViewWynik);
ui->graphicsViewWynik->setScene(scenaWynikowa);
ui->graphicsViewWynik->fitInView(scenaWynikowa->itemsBoundingRect(), Qt::KeepAspectRatio);

QGraphicsOpacityEffect *poziomPrzezroczystosci = new QGraphicsOpacityEffect();
poziomPrzezroczystosci->setOpacity(0.5);

QImage obraz1(s1);
obraz1.scaled(QSize(ui->graphicsViewWynik->width(), ui->graphicsViewWynik->height()), Qt::IgnoreAspectRatio, Qt::FastTransformation);
update();
resize(ui->graphicsViewWynik->width(), ui->graphicsViewWynik->height());
QPixmap mapaPikseli1(n1);
QGraphicsPixmapItem *pixmapItem1 = scenaWynikowa->addPixmap(mapaPikseli1);
QImage obraz2(s2);
obraz2.scaled(QSize(ui->graphicsViewWynik->width(), ui->graphicsViewWynik->height()), Qt::IgnoreAspectRatio, Qt::FastTransformation);
update();
resize(ui->graphicsViewWynik->width(), ui->graphicsViewWynik->height());
QPixmap mapaPikseli2(n2);
QGraphicsPixmapItem *pixmapItem2 = scenaWynikowa->addPixmap(mapaPikseli2);

pixmapItem1->setGraphicsEffect(poziomPrzezroczystosci);
pixmapItem2->setGraphicsEffect(poziomPrzezroczystosci);
pixmapItem2->moveBy(0, 0);

ui->graphicsViewWynik->show();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top