Results 1 to 3 of 3

Thread: best way to load image from db

  1. #1
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question best way to load image from db

    Hi, i'm currently loading images from a sqlite3 blob-field using this code:
    Qt Code:
    1. void ImageViewer::loadImage ( int pic )
    2. {
    3. QSqlQuery query ( QSqlDatabase::database() );
    4. query.prepare ( "SELECT data FROM img_images WHERE id = :id" );
    5. query.bindValue ( ":id", img_list[pic] );
    6. query.exec();
    7. if ( query.next() )
    8. {
    9. return QImage::fromData ( query.value ( 0 ).toByteArray() ) );
    10. }
    11. else
    12. {
    13. return QImage();
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Now i found some examples in one of my Qt-Books:
    Qt Code:
    1. QImage ImageCollection::getImage( int id )
    2. {
    3. QSqlQuery qrx;
    4. qry.prepare ( "SELECT data FROM images WHERE id = :id" );
    5. qry.bindValue ( ":id", id );
    6. if ( !qry.exec() ) qFatal ( "Failed to get image" );
    7. if ( !qry.next() ) qFatal ( "Failed to get image id" );
    8. QByteArray array = qry.value ( 0 ).toByteArray();
    9. QBuffer buffer ( &array );
    10. buffer.open ( QIODevice::ReadOnly );
    11. QImageReader reader ( &buffer, "PNG" );
    12. QImage image = reader.read();
    13. return image;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Which code is better, if i know that i only have one image format in the database and why? which one is the fastest?
    Using Qt4.4b1, 4.4rc1 is compiling atm. System is Debian Sid.
    C167

  2. #2
    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: best way to load image from db

    IMO the first version is better, because it's shorter and supports all possible images formats. It might be a bit slower, because Qt must detect the format, but I don't think you will notice the difference.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: best way to load image from db

    Hi,

    because of QImage::fromData is defined as
    Qt Code:
    1. QImage QImage::fromData(const uchar *data, int size, const char *format)
    2. {
    3. QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(data), size);
    4. b.setData(a);
    5. b.open(QIODevice::ReadOnly);
    6. return QImageReader(&b, format).read();
    7. }
    To copy to clipboard, switch view to plain text mode 
    both are equal. But as jacek mentioned your code is shorter in "front end", so I would prefer it.

    If you need speed improvement store all images in the database in the same image format, and specify it in fromRawData(). Also if img_list is an private member and you definitely know the content you may use
    Qt Code:
    1. query.exec( "SELECT data FROM img_images WHERE id = " + QString::number(img_list[pic]));
    To copy to clipboard, switch view to plain text mode 
    (assuming img_list[pic] will return an integer)

    Lykurg

Similar Threads

  1. [Qt4] How to load Url image into QImage?
    By Gonzalez in forum Qt Programming
    Replies: 6
    Last Post: 13th October 2014, 10:10
  2. how to load a Qt non-supported Image format
    By Nithya in forum Qt Programming
    Replies: 5
    Last Post: 3rd April 2008, 12:57
  3. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  4. Explanation to Image Formats
    By sincnarf in forum Qt Programming
    Replies: 13
    Last Post: 6th July 2007, 17:02
  5. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36

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.