Results 1 to 17 of 17

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

Hybrid View

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

    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 ...

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

    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.

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

    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

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

    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.

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

    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, 10:08
  2. How to tell if QTableView cell is visible ?
    By steviekm3 in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2009, 10:04
  3. How to tell if cell in QTableView is visible ??
    By steviekm3 in forum Qt Programming
    Replies: 3
    Last Post: 27th February 2009, 17:57
  4. how to get data from cell of QTableview
    By yleesun in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 14:31
  5. cell selection in QTableView
    By user in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2008, 01: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
  •  
Qt is a trademark of The Qt Company.