rav
13th May 2011, 13:11
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:
//when loading an image
imagePix = new QPixmap(fileName);
scene->clear();
image = new QGraphicsPixmapItem( *imagePix );
scene->addItem( image );
...
//when displaying the image (and processing it)
if(imagePix == NULL)
return;
//if(mask1 != NULL){
qDebug()<<"here0";
scene->removeItem(mask1);
//delete mask1;
//}
qDebug()<<"here1";
int w = imagePix->width();
int h = imagePix->height();
QPixmap *temp = new QPixmap( w,h );
QPainter painter(temp);
painter.setPen( Qt::black );
painter.setBrush( Qt::black );
painter.drawRect(imagePix->rect());
painter.end();
mask1Pix = new QImage(w,h, QImage::Format_RGB888);
mask1Pix->fill(qRgb(255,255,255));
ui->bar->setVisible(true);
ui->bar->setValue(0);
int pix = w*h;
QImage src = imagePix->toImage();
//here paint what areas to reveal--------------------------------------------------
for(int i=0; i<w; i++){
for(int j=0; j<h; j++){
QRgb p = src.pixel(i,j);
if( qRed(p) - qBlue(p) - qGreen(p) >= 10){
mask1Pix->setPixel( i,j, qRgb(0,0,0) ); //where red - will be transparent
}
}
ui->bar->setValue(((i+1)*h*100)/pix);
}
//--------------------------------------------------------------------------------
ui->bar->setValue(0);
ui->bar->setVisible(false);
temp->setAlphaChannel( QPixmap::fromImage( *mask1Pix ) );
mask1 = new QGraphicsPixmapItem( *temp );
mask1->setZValue(2);
mask1->setOpacity(0.9f);
scene->addItem(mask1);
qDebug() << imagePix << mask1Pix << "\n"<< image << mask1;
delete temp;
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
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:
//when loading an image
imagePix = new QPixmap(fileName);
scene->clear();
image = new QGraphicsPixmapItem( *imagePix );
scene->addItem( image );
...
//when displaying the image (and processing it)
if(imagePix == NULL)
return;
//if(mask1 != NULL){
qDebug()<<"here0";
scene->removeItem(mask1);
//delete mask1;
//}
qDebug()<<"here1";
int w = imagePix->width();
int h = imagePix->height();
QPixmap *temp = new QPixmap( w,h );
QPainter painter(temp);
painter.setPen( Qt::black );
painter.setBrush( Qt::black );
painter.drawRect(imagePix->rect());
painter.end();
mask1Pix = new QImage(w,h, QImage::Format_RGB888);
mask1Pix->fill(qRgb(255,255,255));
ui->bar->setVisible(true);
ui->bar->setValue(0);
int pix = w*h;
QImage src = imagePix->toImage();
//here paint what areas to reveal--------------------------------------------------
for(int i=0; i<w; i++){
for(int j=0; j<h; j++){
QRgb p = src.pixel(i,j);
if( qRed(p) - qBlue(p) - qGreen(p) >= 10){
mask1Pix->setPixel( i,j, qRgb(0,0,0) ); //where red - will be transparent
}
}
ui->bar->setValue(((i+1)*h*100)/pix);
}
//--------------------------------------------------------------------------------
ui->bar->setValue(0);
ui->bar->setVisible(false);
temp->setAlphaChannel( QPixmap::fromImage( *mask1Pix ) );
mask1 = new QGraphicsPixmapItem( *temp );
mask1->setZValue(2);
mask1->setOpacity(0.9f);
scene->addItem(mask1);
qDebug() << imagePix << mask1Pix << "\n"<< image << mask1;
delete temp;
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