Results 1 to 5 of 5

Thread: Save images to database

  1. #1
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Save images to database

    QT4 windows

    I would like to save an image file (jpg,bmp...) into an sqlite database as a BLOB. What format must it be in to save it to the database? I can't find anything that converts into a ByteArray.

    Once that problem is solved I will then have to view the image form the database. So whatever format i save it in I will have to be able to put it back into QImage or QPixmap.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save images to database

    If you want Qt to decode and/or encode it, the format should be on this list.
    If you just need the data from an image file, QFile::readAll() should be enough. If you want to encode the image in memory, you need the following:

    Qt Code:
    1. QBuffer buffer;
    2. QImageWriter writer(&buffer, "PNG");
    3. writer.write(image);
    4. QByteArray data = buffer.data();
    To copy to clipboard, switch view to plain text mode 

    To decode:

    Qt Code:
    1. QByteArray array = variant.toByteArray();
    2. QBuffer buffer(&array);
    3. QImageReader reader(&buffer, "PNG");
    4. QImage image = reader.read();
    To copy to clipboard, switch view to plain text mode 

    I'm assuming you know how to insert the QByteArray into the database and get it out, but If you any clarification, just ask.
    Last edited by init2null; 5th May 2006 at 02:56.
    ImageRocket - My Qt4-based image editing program
    Project Page / Screenshots / Source

  3. The following user says thank you to init2null for this useful post:

    jnk5y (5th May 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save images to database

    It should be possible to convert between QVariant and QImage with a single line of code.
    Last edited by jacek; 5th May 2006 at 20:28. Reason: fixed typo

  5. #4
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save images to database

    Thanks init2null. I actually did it a similar way. I used QFile::readAll() and stored that into the database. Then used QImage::loadFromData() to put into memory.

    Now I want to view it and I want to display it with a few lineEdits and a couple of buttons in a QMainWindow. Unfortunately the widget I want to display it on is not giving me back it's proper size in order to scale the image. Do you know why it would do that?

  6. #5
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save images to database

    Quote Originally Posted by jnk5y
    Thanks init2null. I actually did it a similar way. I used QFile::readAll() and stored that into the database. Then used QImage::loadFromData() to put into memory.

    Now I want to view it and I want to display it with a few lineEdits and a couple of buttons in a QMainWindow. Unfortunately the widget I want to display it on is not giving me back it's proper size in order to scale the image. Do you know why it would do that?
    Widgets do not always correctly report their sizes until just before they are shown (particularly when they are used in layouts). Defer your code until the showEvent() to get accurate widget sizes.
    Save yourself some pain. Learn C++ before learning Qt.

Similar Threads

  1. how to save database
    By khalid_se in forum Newbie
    Replies: 7
    Last Post: 10th July 2011, 18:47
  2. Replies: 2
    Last Post: 8th August 2008, 01:54
  3. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  4. How can I save a QImage object into a SQLite3 database table?
    By danielperaza in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2008, 05:39
  5. Putting and getting images from MySQL database?
    By allensr in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2007, 21:47

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.