Results 1 to 10 of 10

Thread: Question about QImage

  1. #1
    Join Date
    Oct 2009
    Posts
    70

    Default Question about QImage

    Hi, I've a question about QImage.

    I create a QImage, passing a uchar * buffer.

    This QImage, is a property of a class.

    When I destroy the object of this class, I need to destroy the buffer of the QImage.

    How can I get this buffer from the QImage ?

    bits() return a deep copy.

    I think that it's possible to get the original buffer, without save it as property of the class.

    Many thanks,

    Paolo

  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: Question about QImage

    The const bits() function does not return a deep copy

  3. #3
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Question about QImage

    How can I call the bits() const instead the bits() function ?

  4. #4
    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: Question about QImage

    Using const uchar * of course.

    But:
    I would probably do this differently. I would keep track of the data and delete it at the appropriate place.
    You could keep a reference list of data and images. When the reference drops below 0, delete the data.

  5. #5
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Question about QImage

    Sorry, but I want to understand a thing.

    How can I call the overaloaded function bits() const instead the bits()?!?!

    It depends about the constructor of the qimage??

    Ex.

    qimage.bits() which values returns ?

  6. #6
    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: Question about QImage

    Qt Code:
    1. uchar *data = myImage.bits(); // Deep copy
    2. const uchar *data = myImage.bits(); // No deep copy
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2010
    Posts
    7
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: Question about QImage

    Just to be clear, the non-const bits() method does not create a deep copy of the memory unless there are other copies of the image sharing the same data. All it does is call detach(). If you are sure that that's the only copy of the image then calling it will work. If not then you cannot safely delete the memory in any case. I would agree that there are better ways to do this. In most situations the best thing to do is to allow QImage to manage its own memory.

  8. #8
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Question about QImage

    Just to be clear, the non-const bits() method does not create a deep copy of the memory unless there are other copies of the image sharing the same data. All it does is call detach(). If you are sure that that's the only copy of the image then calling it will work. If not then you cannot safely delete the memory in any case. I would agree that there are better ways to do this. In most situations the best thing to do is to allow QImage to manage its own memory.
    How is it possible to allow QImage to manage its own memory?

  9. #9
    Join Date
    Sep 2010
    Posts
    7
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: Question about QImage

    Quote Originally Posted by paolom View Post
    How is it possible to allow QImage to manage its own memory?
    Don't use the constructor that takes an existing buffer. If you use e.g. QImage ( int width, int height, Format format ) it will manage its own memory. Obviously this is sub-optimal if you happen to already have the image data in memory, but it will make resource management much easier.

  10. #10
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Question about QImage

    Don't use the constructor that takes an existing buffer. If you use e.g. QImage ( int width, int height, Format format ) it will manage its own memory. Obviously this is sub-optimal if you happen to already have the image data in memory, but it will make resource management much easie
    I've an existing buffer in memory.
    I can use the QImage ( int width, int height, Format format ) constructor also, but how I can fill the qimage with my buffer and leave the QImage manage its own memory ?!

    Ex. setPixel() ???????

Similar Threads

  1. QImage
    By andreahmed in forum Qt Programming
    Replies: 5
    Last Post: 25th August 2010, 07:11
  2. QImage and Qt 4.5.3
    By halmassi in forum Newbie
    Replies: 26
    Last Post: 10th March 2010, 21:49
  3. QImage
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 17th October 2007, 20:35
  4. What's faster: QPixmap-to-QImage or QImage-to-QPixmap
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2006, 17:11
  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.