Hi!

I am experiencing some crazy issues with graphics view.
I am trying to create an image viewer.

What I am experiencing is mixing of pointers.

The code goes like this:

Qt Code:
  1. //when loading an image
  2. imagePix = new QPixmap(fileName);
  3. scene->clear();
  4. image = new QGraphicsPixmapItem( *imagePix );
  5. scene->addItem( image );
  6.  
  7. ...
  8.  
  9. //when displaying the image (and processing it)
  10. if(imagePix == NULL)
  11. return;
  12.  
  13. //if(mask1 != NULL){
  14. qDebug()<<"here0";
  15. scene->removeItem(mask1);
  16.  
  17. //delete mask1;
  18. //}
  19. qDebug()<<"here1";
  20. int w = imagePix->width();
  21. int h = imagePix->height();
  22.  
  23. QPixmap *temp = new QPixmap( w,h );
  24. QPainter painter(temp);
  25. painter.setPen( Qt::black );
  26. painter.setBrush( Qt::black );
  27. painter.drawRect(imagePix->rect());
  28. painter.end();
  29.  
  30. mask1Pix = new QImage(w,h, QImage::Format_RGB888);
  31. mask1Pix->fill(qRgb(255,255,255));
  32.  
  33. ui->bar->setVisible(true);
  34. ui->bar->setValue(0);
  35.  
  36. int pix = w*h;
  37. QImage src = imagePix->toImage();
  38.  
  39. //here paint what areas to reveal--------------------------------------------------
  40. for(int i=0; i<w; i++){
  41. for(int j=0; j<h; j++){
  42. QRgb p = src.pixel(i,j);
  43. if( qRed(p) - qBlue(p) - qGreen(p) >= 10){
  44. mask1Pix->setPixel( i,j, qRgb(0,0,0) ); //where red - will be transparent
  45. }
  46. }
  47. ui->bar->setValue(((i+1)*h*100)/pix);
  48. }
  49. //--------------------------------------------------------------------------------
  50. ui->bar->setValue(0);
  51. ui->bar->setVisible(false);
  52.  
  53.  
  54. temp->setAlphaChannel( QPixmap::fromImage( *mask1Pix ) );
  55.  
  56. mask1 = new QGraphicsPixmapItem( *temp );
  57. mask1->setZValue(2);
  58. mask1->setOpacity(0.9f);
  59.  
  60. scene->addItem(mask1);
  61.  
  62. qDebug() << imagePix << mask1Pix << "\n"<< image << mask1;
  63.  
  64. delete temp;
To copy to clipboard, switch view to plain text mode 

Please ignore the large ammount of comments - I was trying to check everything before posting here.

The app creates a second item (mask1), based on the red channel of the original pic.

The issue is as follows:
- first time I load a pic - works great
- second time i load a pic (i.e. the same one) - it all mixes up - the "picture" is being replaced with the mask (check the qDebug() output - pointer values). Instead of seeing the Pic WITH a second semi-transparent layer, i see only the second layer. Why?

I tried deleting whatever i allocate, no effect. My very temporary solution is allocating everything multiple times, the pointers differ then.

What am I missing?

Regards,
Rav