Results 1 to 3 of 3

Thread: Problem using GraphicsView

  1. #1
    Join Date
    May 2011
    Posts
    2
    Platforms
    Windows Symbian S60

    Question Problem using GraphicsView

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem using GraphicsView

    First of all simplify your code. Never allocate objects of type QImage or QPixmap on the heap (using "new") but rather on the stack. This will already significantly improve your code and it will be harder for you to "mix" pointers.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2011
    Posts
    2
    Platforms
    Windows Symbian S60

    Default Re: Problem using GraphicsView

    Problem solved.

    Solution to it:
    - I used scene->clear() which deleted everything
    - then I used scene->removeItem( ...) where I place an item that is supposed to be removed already in clear()

    I guess the order of deleting/creating items was so unfortunate, that Qt deleted my new objects ;-)

    Thanks anyway!

Similar Threads

  1. GraphicsView mousePressEvent problem
    By sergiofil in forum Newbie
    Replies: 4
    Last Post: 16th December 2010, 11:30
  2. The problem when add QAxWidget into graphicsView.
    By Scott Liu in forum Qt Programming
    Replies: 8
    Last Post: 9th September 2009, 06:39
  3. graphicsview Problem
    By tampstaffs in forum Qt Programming
    Replies: 6
    Last Post: 8th February 2009, 19:59
  4. QSound Problem with GraphicsView
    By QbelcorT in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 14th January 2009, 12:28
  5. GraphicsView rotate problem
    By forrestfsu in forum Qt Programming
    Replies: 7
    Last Post: 21st November 2007, 20:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.