Results 1 to 17 of 17

Thread: How do I display a picture on a QTableView cell?

  1. #1
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Thanks
    4
    Platforms
    MacOS X Unix/X11

    Default How do I display a picture on a QTableView cell?

    Hi, it's me again !

    I have a database table in which one of its columns stores the path to an image. I want to use a QSqlTableModel and a QTableView to show the records stored in that table, but I want to show the actual picture instead of its path. I'm a little confused by Qt's documentation on this issue.

    Currently I'm trying to set the ItemDataRole to DecorationRole as it follows:

    Qt Code:
    1. this->_model->setHeaderData(5, Qt::Horizontal, "Photo", Qt::DecorationRole);
    To copy to clipboard, switch view to plain text mode 

    But it doesn't work. Isn't any other way to do it without creating a custom delegate?

    I looking at this example anyway, while this post gets replied:

    http://doc.qt.nokia.com/4.6/itemviews-stardelegate.html

  2. #2
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Thanks
    4
    Platforms
    MacOS X Unix/X11

    Default Re: How do I display a picture on a QTableView cell?

    OK I followed the example above, and now I'm able to render the picture instead of its file path. Now I want to render it with a proper size, since it's being currently rendered at the size of its containing row. I want the row's height expands as needed to give the picture a proportional size (width is constrained).

  3. #3
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I display a picture on a QTableView cell?

    There is no need to use delegates, when you don't need to EDIT items. Your model must contain QAbstractItemModel::data function. When view is asking to model a Qt:ecorationRole - return a QPixmap, when asking Qt::SizeHintRole - return QSize, that you counted for cell
    see ItemDataRole

  4. #4
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Thanks
    4
    Platforms
    MacOS X Unix/X11

    Default Re: How do I display a picture on a QTableView cell?

    So that means I must subclass a QSqlTableModel in order to reimplement the data() method, right?

  5. #5
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I display a picture on a QTableView cell?

    So that means I must subclass a QSqlTableModel in order to reimplement the data() method, right?
    absolutely

  6. #6
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Thanks
    4
    Platforms
    MacOS X Unix/X11

    Default Re: How do I display a picture on a QTableView cell?

    Ok, I'm trying the following snippet but it doesn't seem to work (I receive Segmentation Fault):

    Qt Code:
    1. QVariant PersonasModel::data(const QModelIndex &idx, int role) const
    2. {
    3. if(idx.column() == 5)
    4. {
    5. if(!QFile::exists(idx.data().toString()))
    6. imgFile = ":/centinela/images/picture_unavailable.jpg";
    7.  
    8. QPixmap pixmap(idx.data().toString());
    9.  
    10. if(role == Qt::DecorationRole)
    11. return pixmap;
    12.  
    13. if(role == Qt::SizeHintRole)
    14. return pixmap.size();
    15. return QSqlTableModel::data(idx, role);
    16. }
    17. return QSqlTableModel::data(idx, role);
    18. }
    To copy to clipboard, switch view to plain text mode 

    If instead, I try the following, it does work, but I can't provide the SizeHint:

    Qt Code:
    1. QVariant PersonasModel::data(const QModelIndex &idx, int role) const
    2. {
    3. if(idx.column() == 5 && role == Qt::DecorationRole)
    4. {
    5. return QPixmap(idx.data().toString());
    6. }
    7. return QSqlTableModel::data(idx, role);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Note that the fifth column of my model is the one storing the pictures. And I would like to be able to render a default picture, in case one is not available.

  7. #7
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I display a picture on a QTableView cell?

    The first variant seems to be good, unless unreferenced variable imgFile. Try to debug to see what code causes fault

  8. #8
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Thanks
    4
    Platforms
    MacOS X Unix/X11

    Default Re: How do I display a picture on a QTableView cell?

    Sorry, I mispelled the code that declared imgFile. But I already tried debugging and I'm sure that's not the problem. In my debugging session I noticed that idx.data() seems to fall into a infinite recursion. Somehow from this point on, a segmentation fault is produced. I used Qt Creator with GDB, but I couldn't spot the problem.

  9. #9
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I display a picture on a QTableView cell?

    it seems, that idx.data() calls your PersonasModel::data
    try next:
    Qt Code:
    1. QVariant PersonasModel::data(const QModelIndex &idx, int role) const
    2. {
    3. if ( idx.column() == 5 )
    4. {
    5. QString imgFile = QSqlTableModel::data( idx, Qt::DisplayRole );
    6. if ( Qt::DisplayRole == role )
    7. {
    8. return QString();
    9. }
    10. if ( !QFile::exists( imgFile )
    11. {
    12. imgFile = ":/centinela/images/picture_unavailable.jpg";
    13. }
    14. QPixmap pixmap( imgFile );
    15.  
    16. if ( role == Qt::DecorationRole )
    17. {
    18. return pixmap;
    19. }
    20. if(role == Qt::SizeHintRole)
    21. {
    22. return pixmap.size();
    23. }
    24. }
    25. return QSqlTableModel::data( idx, role );
    26. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How do I display a picture on a QTableView cell?

    idx.data() calls your data implementation. And idx.column() is 5 so it goes to your if (column == 5) and there is a code asking for idx.data() and so on recursively. I suggest changing condition in if from column == 5 to (column == 5 && (role == Qt:ecorationRole || Qt::SizeHintRole)). In your first posted code and it should be fine.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  11. #11
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Thanks
    4
    Platforms
    MacOS X Unix/X11

    Default Re: How do I display a picture on a QTableView cell?

    Ok, thanks a lot for your suggestion. I'll try it out and see if it works.

  12. #12
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Thanks
    4
    Platforms
    MacOS X Unix/X11

    Default Re: How do I display a picture on a QTableView cell?

    I tried this:

    Qt Code:
    1. QVariant PersonasModel::data(const QModelIndex &idx, int role) const
    2. {
    3. if(idx.column() == 5 && (role == Qt::DecorationRole || role == Qt::SizeHintRole/* || role == Qt::DisplayRole*/))
    4. {
    5. QString imgFile = idx.data().toString();
    6. if(!QFile::exists(imgFile))
    7. imgFile = ":/centinela/images/picture_unavailable.jpg";
    8.  
    9. QPixmap pixmap(idx.data().toString());
    10.  
    11. if(role == Qt::DecorationRole)
    12. return pixmap;
    13.  
    14. if(role == Qt::SizeHintRole)
    15. return pixmap.size();
    16.  
    17. /*if(role == Qt::DisplayRole)
    18.   return "";*/
    19. }
    20. return QSqlTableModel::data(idx, role);
    21. }
    To copy to clipboard, switch view to plain text mode 

    It renders the picture, but rows aren't resized to the proper height (should I call QTableView::resizeRowsToContents() ?) and also there is a more important problem: it also renders the picture's file path besides the image; I don't want this to happen. I only want to show the picture. The commented code is what I tried to do in order to fix it, but causes the program to crash again.

  13. #13
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I display a picture on a QTableView cell?

    your program crashes, because you should write
    Qt Code:
    1. QString imgFile = QSqlTableModel::data(idx, Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. QString imgFile = idx.data().toString();
    To copy to clipboard, switch view to plain text mode 
    try to exactly repeat my previous code...
    Qt Code:
    1. QVariant PersonasModel::data(const QModelIndex &idx, int role) const
    2. {
    3. if ( idx.column() == 5 )
    4. {
    5. QString imgFile = QSqlTableModel::data( idx, Qt::DisplayRole );
    6. if ( Qt::DisplayRole == role )
    7. {
    8. return QString();
    9. }
    10. if ( !QFile::exists( imgFile )
    11. {
    12. imgFile = ":/centinela/images/picture_unavailable.jpg";
    13. }
    14. QPixmap pixmap( imgFile );
    15. if ( role == Qt::DecorationRole )
    16. {
    17. return pixmap;
    18. }
    19. if(role == Qt::SizeHintRole)
    20. {
    21. return pixmap.size();
    22. }
    23. }
    24. return QSqlTableModel::data( idx, role );
    25. }
    To copy to clipboard, switch view to plain text mode 
    should I call QTableView::resizeRowsToContents() ?
    honestly, I don't know. try ...

  14. #14
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Thanks
    4
    Platforms
    MacOS X Unix/X11

    Default Re: How do I display a picture on a QTableView cell?

    Worked great!, but there are still two little problems: 1) default picture is never rendered, 2) rows never acquire the required height, not even calling QTableView::resizeRowsToContents() nor without calling it.

  15. #15
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I display a picture on a QTableView cell?

    1)
    Are you sure, that resource you use is available ?
    try
    Qt Code:
    1. QLabel * label = new QLabel( this );
    2. label->setPixmap( QPixmap( ":/centinela/images/picture_unavailable.jpg" ) );
    To copy to clipboard, switch view to plain text mode 
    maybe your resource named :/centinela/images/picture_unavailable without .jpg ?


    2)
    try to comment out
    Qt Code:
    1. if(role == Qt::SizeHintRole)
    2. {
    3. return pixmap.size();
    4. }
    To copy to clipboard, switch view to plain text mode 
    and call resizeRowsToContents

  16. #16
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Thanks
    4
    Platforms
    MacOS X Unix/X11

    Default Re: How do I display a picture on a QTableView cell?

    I was spelling the "image" part of my resource's path in english, but declaring it in spanish ("imagenes"), so that solves the first problem. However, I took into account your second advice, and nothing happened.

  17. #17
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Thanks
    4
    Platforms
    MacOS X Unix/X11

    Default Re: How do I display a picture on a QTableView cell?

    Please, does any body have any idea?

Similar Threads

  1. how to color a cell in a QTableView
    By JeanC in forum Qt Programming
    Replies: 13
    Last Post: 9th September 2015, 11:08
  2. How to tell if QTableView cell is visible ?
    By steviekm3 in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2009, 11:04
  3. How to tell if cell in QTableView is visible ??
    By steviekm3 in forum Qt Programming
    Replies: 3
    Last Post: 27th February 2009, 18:57
  4. how to get data from cell of QTableview
    By yleesun in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 15:31
  5. cell selection in QTableView
    By user in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2008, 02:01

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.