Results 1 to 15 of 15

Thread: Zooming a qimage on a QGraphicsItem

  1. #1
    Join Date
    Oct 2009
    Posts
    70

    Default Zooming a qimage on a QGraphicsItem

    Zooming a qimage
    Hi,

    I've a graphicsItem with a qimage.

    I need zooming this item but i want maintain the original size of the qimage.

    I use the QTransform scale.

    It's ok.

    Now, I need to use the smooth transformation when I zoomIn the item.

    I think it's impossible with QTransform. I try with many way, but I can't do it.

    Does anybody know a way to make this?

    PS. I need to use the QTransform() cause this Item can have a child items, and in this mode I can resize all.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Zooming a qimage on a QGraphicsItem

    May be you can used QStyleOptionGraphicsItem::levelOfDetail and render your item accordingly...

  3. #3
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Zooming a qimage on a QGraphicsItem

    Quote Originally Posted by aamer4yu View Post
    May be you can used QStyleOptionGraphicsItem::levelOfDetail and render your item accordingly...
    Yes, but I need to draw the QImage of the item with the Qt::SmoothTrasformation. The renderHint of the painter doesn't make the correct smoothing.

    Any ideas?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Zooming a qimage on a QGraphicsItem

    Are you scaling the QImage directly,or do you scale your graphics view object which holds your QImage (which I guess is a QPixmap at that point already)?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Zooming a qimage on a QGraphicsItem

    Quote Originally Posted by high_flyer View Post
    Are you scaling the QImage directly,or do you scale your graphics view object which holds your QImage (which I guess is a QPixmap at that point already)?
    No. the QGraphicsItem contains a QImage. The bounding rect of the item is the rect of the QImage.

    I can't use a QGraphicsPixmapItem cause I use this object in a thread.

    I can't use a QPixmap on the GUI ( from a conversion to QImage) cause the time to convert is too much for me.

    My item as a number of child items. I can zoom the image and its children with the transform. and It's very easy! But in this mode I can't smooth the qimage on high resolution cause the size of the qimage is always the same.

    I hope that you can understand my bad english.

    Waiting for you reply

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Zooming a qimage on a QGraphicsItem

    the QGraphicsItem contains a QImage.
    This is something I don't quite understand, since QImage is not a visible object.
    Can you show the relevant code of the QImage integration,and its painting?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Zooming a qimage on a QGraphicsItem

    Quote Originally Posted by high_flyer View Post
    This is something I don't quite understand, since QImage is not a visible object.
    Can you show the relevant code of the QImage integration,and its painting?
    Qt Code:
    1. void ImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. Q_UNUSED ( option )
    4. Q_UNUSED ( widget )
    5.  
    6. // Draw the image
    7.  
    8. painter->drawImage(_rect,_qImage); // _qImage is a member of the class
    9. }
    10.  
    11. QRectF ImageItem::boundingRect() const
    12. {
    13. return QRectF(_rect); // _rect is the rect of the QImage
    14. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Zooming a qimage on a QGraphicsItem

    I see.
    I think the problem has to do with the fact, that _rect does not change, and QGraphics View just scales what ever is already drawn.
    So if the original QImage is drawn in factor X,and later you scale your item x2 for example, it will just stretch the small QImage.
    Thats because the size of the item has not changed. (scaling in the scene doesn't change the size of the items)
    You have to scale your image when you scale your item, and force the item to redraw,this will probably give you what you want.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Zooming a qimage on a QGraphicsItem

    Quote Originally Posted by high_flyer View Post
    I see.
    I think the problem has to do with the fact, that _rect does not change, and QGraphics View just scales what ever is already drawn.
    So if the original QImage is drawn in factor X,and later you scale your item x2 for example, it will just stretch the small QImage.
    Thats because the size of the item has not changed. (scaling in the scene doesn't change the size of the items)
    You have to scale your image when you scale your item, and force the item to redraw,this will probably give you what you want.
    Yes, in fact my problem is this!!

    To scale an Item and It's children I use the QTransform.

    With the QTransform my item ( so the QImage ) will scaled without smoothing cause the scale transform doesn't change the size of the qimage.

    The advantage of the use of the QTransform is that it scale the children ( text item or line item ) automatically.

    If I don't use the QTransform to scale my item and I scale the QImage manually ( with the scaled function ) I obtain the item with the smoothing image, but in this mode the children doesn't scale !!!
    I need to scale him manually !!!

    My question is: is it possible to use the QTransform whit an Item with QImage and obtain in some way the smoothing ???

    I hope now it's all more clear.

    Thanks

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Zooming a qimage on a QGraphicsItem

    The Graphics View scaling is only a "view effect",it doesn't change the sizes of the items, it only calculates (behind the scenes) how to draw them in any given factor.
    You have to resize your QImage manually if you want the QImage to be correctly drawn.
    What you can do for that would be to use the various mapToScene() mapFromScene() etc, to get the real pixel size of the rect, and give that rect to the painter.
    Again, you will have to force the redraw of your QImage with the new rect for that, every time you scale, at least when you scale to a larger factor.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Zooming a qimage on a QGraphicsItem

    It just occurred to me, that you can do something else:
    You can draw your QImage in its largest possible size, then it will always look correct, unless you scale it beyond its real size.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #12
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Zooming a qimage on a QGraphicsItem

    Quote Originally Posted by high_flyer View Post
    It just occurred to me, that you can do something else:
    You can draw your QImage in its largest possible size, then it will always look correct, unless you scale it beyond its real size.
    It's a solution, but the occupation in RAM is very high !

  13. #13
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Zooming a qimage on a QGraphicsItem

    Then just use the larges possible image that your screen can show.
    If its not a BMP, it should not be larger the 500KB in average, and any modern PC can handle it.

    If its not an option for you, then you have to do what I wrote before.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #14
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Zooming a qimage on a QGraphicsItem

    I obtain a solution with this code. Do you think is it a possible good solution for my goal?

    Qt Code:
    1. QVariant ImageItem::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. if(ItemTransformHasChanged == change && parentItem() )
    4. {
    5. QTransform trans = transform();
    6. QRectF rect = trans.mapRect(boundingRect());
    7.  
    8. if ( rect.size() != _rect.size() )
    9. {
    10. // getOriginalQImage(false,false) return the original size QImage
    11.  
    12. _qImage = getOriginalQImage(false,false).scaled(rect.size().toSize(),
    13. Qt::KeepAspectRatio,
    14. Qt::SmoothTransformation);
    15. }
    16.  
    17. }
    18.  
    19. return QGraphicsItem::itemChange(change,value);
    20. }
    21.  
    22. void ImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    23. {
    24. Q_UNUSED ( option )
    25. Q_UNUSED ( widget )
    26.  
    27. // Draw the image
    28.  
    29. painter->drawImage(_rect,_qImage); // _qImage is a member of the class
    30. }
    31.  
    32. QRectF ImageItem::boundingRect() const
    33. {
    34. return QRectF(_rect); // _rect is the rect of the QImage
    35. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Zooming a qimage on a QGraphicsItem

    Its hard for me to say if it is good, since I don't know what the application restrictions are.
    But it looks ok, and if it works for you, then great!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 4
    Last Post: 8th May 2009, 12:04
  2. zooming only x
    By mastupristi in forum Qwt
    Replies: 1
    Last Post: 7th May 2009, 08:23
  3. Zooming
    By td in forum Newbie
    Replies: 1
    Last Post: 26th November 2008, 13:43
  4. Zooming..
    By chethana in forum Qt Programming
    Replies: 7
    Last Post: 10th October 2007, 08:17
  5. Replies: 3
    Last Post: 15th March 2006, 11:44

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.