Results 1 to 4 of 4

Thread: problem with Qgraphicspixmapitem

  1. #1
    Join Date
    Oct 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default problem with Qgraphicspixmapitem

    Hi all,

    I need a QGraphicsPixmapItem that show pixel's value when you zoom. So I create my QGraphicsPixmapItem and reimplement paint method :
    Qt Code:
    1. MyPixmapGraphicsItem::MyPixmapGraphicsItem(QPixmap p , QGraphicsItem * parent)
    2. : pix(p)
    3. , QGraphicsItem(parent)
    4. {
    5. image = pix.toImage();
    6. }
    7.  
    8.  
    9. void MyPixmapGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    10. {
    11. const qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());
    12.  
    13. painter->drawPixmap (0,0, pix);
    14.  
    15. if (lod >= 30)
    16. {
    17. QPen pen;
    18. int wStart, wLength;
    19. int hStart, hLength;
    20. int x1,y1;
    21. QRgb pixval;
    22. QString tr,tg,tb;
    23. QRectF boundbox = option->exposedRect;
    24.  
    25. wStart = (int) (boundbox.x());
    26. hStart = (int) (boundbox.y());
    27. wLength = (int) (boundbox.width());
    28. hLength = (int) (boundbox.height());
    29.  
    30. for(int i=hStart ; i<hStart+hLength ; i++)
    31. {
    32. for(int j=wStart ; j<wStart+wLength ; j++)
    33. {
    34. pixval=20;image.pixel(j,i);
    35. tr.setNum(qRed(pixval));
    36. tg.setNum(qGreen(pixval));
    37. tb.setNum(qBlue(pixval));
    38. if(pix.depth()==8)
    39. {
    40. if(qRed(pixval)<140)
    41. pen.setColor( QColor::fromRgb(255,255,255));
    42. else
    43. pen.setColor( QColor::fromRgb(0,0,0) );
    44.  
    45. painter->setPen( pen );
    46. painter->drawText(j,(int) i,tr);
    47. }
    48. else
    49. {
    50. QColor color = QColor::fromRgb(pixval);
    51. if(color.value()<140)
    52. pen.setColor( QColor::fromRgb(255,255,255));
    53. else
    54. pen.setColor( QColor::fromRgb(0,0,0));
    55. painter->setPen( pen );
    56. tr.insert(0,"R:");tg.insert(0,"G:");tb.insert(0,"B:");
    57. painter->drawText(j,i+2,tr);
    58. painter->drawText(j,i+3,tg);
    59. painter->drawText(j,i+4,tb);
    60. }
    61. }
    62. }
    63. }
    64. }
    To copy to clipboard, switch view to plain text mode 

    "Pix" variable is my pixmap and "image" is a Qimage (pix copy) created by MyPixmapGraphicsItem constructor.

    This code not work because I draw pixel value on the entire image. How can I draw pixel value on the display window only.

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: problem with Qgraphicspixmapitem

    Quote Originally Posted by skab View Post
    This code not work because I draw pixel value on the entire image. How can I draw pixel value on the display window only.
    Can you explain the difference between "on the entire image" and "on the display window only"?

  3. #3
    Join Date
    Oct 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem with Qgraphicspixmapitem

    "display windows only" : When you zoom, scrollbar appears and you only see a small part of the entire image. I want to refresh pixel value only on that.

    Currently, I think that is too long to refresh all the image (it freezes my application).

  4. #4
    Join Date
    Oct 2010
    Posts
    37
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem with Qgraphicspixmapitem

    here's how I would do it:
    subclass QGraphicsView and override paint(). Draw your number in that function and then call QGraphicsView:aint() to draw the zoomed scene.

Similar Threads

  1. Simplest example for QGraphicsPixmapItem
    By sincnarf in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2012, 14:24
  2. Replies: 3
    Last Post: 22nd August 2010, 09:23
  3. Using QPropertyAnimation with QGraphicsPixmapItem
    By Luc4 in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2010, 09:47
  4. QGraphicsPixmapItem to QGraphicsScene
    By strateng in forum Newbie
    Replies: 5
    Last Post: 27th March 2010, 01:50
  5. QGraphicsPixmapItem performance
    By cerber in forum Qt Programming
    Replies: 21
    Last Post: 22nd August 2007, 13:41

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.