Results 1 to 6 of 6

Thread: Storing/retrieving a QIcon from a database

  1. #1
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Storing/retrieving a QIcon from a database

    Hello all... I've got an application in which I want to be able to store a few icons in a database (so all clients can use the same icon set, regardless of location). I thought I had worked out a means of accomplishing this, but it appears not... I only get a corrupted image on restore. Basically, I convert the QIcon to a QPixmap, which I then convert to a QImage, which I then call the bits function on, which I then pass to the QByteArray constructor (after casting to a const char *).

    Pretty convoluted, and I suspect that I may have misread the docs at some point along the way. Here's what I'm doing specifically in code:
    Recording an icon:
    Qt Code:
    1. q.prepare("UPDATE discs SET typeicon=:icon WHERE library=:id AND slot=:slot");
    2. QImage image(m_typeIcon.pixmap(QSize(32, 32)).toImage());
    3. QByteArray ba((const char *)image.bits(), image.numBytes());
    4. q.bindValue(":icon", ba);
    To copy to clipboard, switch view to plain text mode 
    where q is a QSqlQuery object and m_typeIcon is a QIcon object.

    Reading an icon:
    Qt Code:
    1. m_typeIcon = QIcon(QPixmap::fromImage(QImage((const uchar *)q.value(3).toByteArray().constData(), 32, 32, QImage::Format_RGB32)));
    To copy to clipboard, switch view to plain text mode 
    Again, q is a QSqlQuery object, and the icon is in column 3 of a select statement.

    The data itself is stored in the database as a varbinary(32000).
    Life without passion is death in disguise

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Storing/retrieving a QIcon from a database

    To store a QImage in a QByteArray write
    Qt Code:
    1. QByteArray bArray;
    2. QBuffer buffer(&bArray);
    3. buffer.open(QIODevice::WriteOnly);
    4.  
    5. QDataStream out(&buffer)
    6. out << image;
    7.  
    8. buffer.close()
    To copy to clipboard, switch view to plain text mode 

    To read a QImage from a QByteArray write
    Qt Code:
    1. QByteArray bArray;
    2. QBuffer buffer(&bArray);
    3. buffer.open(QIODevice::ReadOnly);
    4.  
    5. QDataStream in(&buffer)
    6. in >> image;
    7.  
    8. buffer.close()
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Storing/retrieving a QIcon from a database

    Thanks, I'll give this a try
    Life without passion is death in disguise

  4. #4
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Storing/retrieving a QIcon from a database

    Or you could store in database PATH to picture, not picture itself, like I did ...
    Qt 5.3 Opensource & Creator 3.1.2

  5. #5
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Storing/retrieving a QIcon from a database

    Quote Originally Posted by MarkoSan View Post
    Or you could store in database PATH to picture, not picture itself, like I did ...
    That wouldn't work - not all machines have access to the same path, but all machines will have access to the same database.
    Life without passion is death in disguise

  6. #6
    Join Date
    Dec 2007
    Posts
    14
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Storing/retrieving a QIcon from a database

    why don't you store a blob of a full png file then creates the icon from the png ?

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  3. Database Master-Detail Entry Form
    By Phan Sin Tian in forum Newbie
    Replies: 4
    Last Post: 3rd February 2008, 14:31
  4. Make error with Qt Jambi 4.3.2_01
    By pamalite in forum Installation and Deployment
    Replies: 0
    Last Post: 22nd November 2007, 12:05
  5. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53

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.