Results 1 to 3 of 3

Thread: How to show images from database as icons in QListView

  1. #1
    Join Date
    Jan 2011
    Posts
    2
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default How to show images from database as icons in QListView

    Hi guys,

    I have a database which stores images as BLOB field.
    What I'm trying to achieve is to show those images (thumbnails of them) as icons in QListView. I use QSqlQueryModel as a model to access the database.

    Can anyone give me any idea how to implement that? If I chose wrong classes, can you suggest better ones for that purpose?

    Thanks,
    Alex

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to show images from database as icons in QListView

    Is there anything in particular you are having problems with?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2011
    Posts
    2
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: How to show images from database as icons in QListView

    Hi wysota, thanks for reply.
    I just had no idea how to implement it.
    Now I have done it the following way:

    I've created a new model class based on QSqlQueryModel and implemented data() method:

    Qt Code:
    1. QVariant SqlQueryModel::data ( const QModelIndex & item, int role) const
    2. {
    3. if (role == Qt::DecorationRole)
    4. {
    5. switch(item.column())
    6. {
    7. case 0: // column 0 contains text data
    8. QPixmap image;
    9. image.loadFromData(record(item.row()).value(1).toByteArray()); // column 1 contains BLOB data
    10. return image.scaled(160, 160, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    11. }
    12. }
    13. return QSqlQueryModel::data(item, role);
    14. }
    To copy to clipboard, switch view to plain text mode 

    Then I connected it to QListView like this:
    Qt Code:
    1. m_pQueryModel = new SqlQueryModel(this);
    2. m_pQueryModel->setQuery(m_sQuery, m_sqlDatabase);
    3. QListView *pView = new QListView(ui.tab_2);
    4. pView->setViewMode(QListView::IconMode);
    5. pView->setModelColumn(0);
    6. pView->setModel(m_pQueryModel);
    To copy to clipboard, switch view to plain text mode 

    I'm curious whether it's correct solution or it can be improved somehow.
    Thanks,
    Alex

Similar Threads

  1. Replies: 6
    Last Post: 30th December 2010, 11:19
  2. QT QListView (as icons)
    By Majdi in forum Newbie
    Replies: 10
    Last Post: 2nd March 2010, 23:22
  3. QListView Icons with uniform sizes
    By psih128 in forum Qt Programming
    Replies: 2
    Last Post: 23rd November 2009, 10:25
  4. how to put different sized icons in a QListView
    By namume in forum Qt Programming
    Replies: 1
    Last Post: 5th February 2008, 10:57
  5. Images/icons. It's not displayed
    By LMZ in forum Qt Tools
    Replies: 37
    Last Post: 18th May 2007, 14:55

Tags for this Thread

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.