Results 1 to 5 of 5

Thread: image saving and retrieving in database using Qt with C++

  1. #1
    Join Date
    Dec 2015
    Posts
    5
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default image saving and retrieving in database using Qt with C++

    Creating For All,
    I Have an entry form created in Qt for entering and retrieving data of employees, including the employee photo and I have a problem in saving and retrieving the image to and from the database which is SQL server 2008 database, can any one help achieve this goal!!!.

    Thanks in advance،،،،،

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: image saving and retrieving in database using Qt with C++

    Convert the picture to QImage if you don't have it already. You can convert a JPG or other file into QImage if you have the photo as a disk file. See the QImage documentation on "Reading and Writing Image Files" to make sure you have the proper plugins for the picture format.

    To store in the database, your DB needs to have a BLOB field in the employee record. You can obtain a pointer to the pixels in the image (as unsigned char *) using QImage::constBits() and QImage::byteCount(). You can then store this in your BLOB.

    To retrieve from the database, you retrieve the BLOB into an unsigned char array, and pass that and the size into the static QImage::fromData() method. This gives you the same QImage that you stored, unless you or the database did something to mangle the bits.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    Khalifa (17th April 2017)

  4. #3
    Join Date
    Dec 2015
    Posts
    5
    Thanks
    3
    Qt products
    Qt5 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: image saving and retrieving in database using Qt with C++

    thank you for your reply, but I'm using SQL Server 2008 database, and I didn't find data type called BLOB, in stead i found 'image' data type, will it do in the place of BLOB??

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: image saving and retrieving in database using Qt with C++

    "BLOB" is an acronym for "binary large object" and is usually a variable-length binary array of bytes. In SQL Server this can be the "binary", "varbinary", "varbinary max", or "image" type. In SQL Server 2008, the "image" data type is deprecated so you should not use it for new work. You should use "varbinary max" instead. This will allow you to store images of up to 2^31 bytes. If you can guarantee that your photos will always be less than 8000 bytes, you can use "varbinary" instead. 8000 bytes is a very small image, so I think I would use "varbinary max". See here.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    Khalifa (23rd April 2017)

  7. #5
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: image saving and retrieving in database using Qt with C++

    Unless you are constrained to storing the actual images in the database, consider storing their locations (i.e., path or URL), which are just strings.

Similar Threads

  1. Replies: 10
    Last Post: 18th October 2012, 10:20
  2. Saving parts of an SQLITE database to a new file
    By zarkzervo in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2010, 00:59
  3. Problem saving txtfile contents to database
    By cutie.monkey in forum Qt Programming
    Replies: 0
    Last Post: 6th January 2010, 07:11
  4. Problem saving JPG image
    By avis_phoenix in forum Newbie
    Replies: 1
    Last Post: 31st July 2009, 15:38
  5. Storing/retrieving a QIcon from a database
    By KShots in forum Qt Programming
    Replies: 5
    Last Post: 1st June 2008, 02:55

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.