Results 1 to 8 of 8

Thread: tablewidget and pixmap

  1. #1
    Join Date
    Nov 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default tablewidget and pixmap

    Hi, using Qt 4.2.1, I'd like to display images on one of the fourth column of my tablewidget. Anyone could give me hints on how I can do this ? it's a display-only table.

    thanks

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

    Default Re: tablewidget and pixmap

    Set the icon of the item to the pixmap you wish to display (using QTableWidgetItem::setIcon().

  3. The following 2 users say thank you to wysota for this useful post:

    chshlin (3rd November 2010), magoo (8th November 2006)

  4. #3
    Join Date
    Nov 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: tablewidget and pixmap

    yes, but i'd like to display the pixmap at a fixed size and not a tiny icon... how can I do this ?

    many thanks

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

    Default Re: tablewidget and pixmap

    Provide a new item delegate which will render your pixmap and return a proper size for the item so that the pixmap can fit it. See QAbstractItemDelegate for more info.

  6. The following user says thank you to wysota for this useful post:

    magoo (8th November 2006)

  7. #5
    Join Date
    Nov 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: tablewidget and pixmap

    that's cool and I was able to make it works. Another problem I have, why do I have to call resizeColumnsToContents() and resizeRowsToContents() if I add more than one rows to my TableWidget ? The cells size seems to reset after I add new TableWidgetItem to the table... There's probably a way for the table to remember the size of the cells that are already in the table...

    Also, calling scrollToBottom() doesn't actually scroll anything...

    as an example :

    mainwindow.cpp :
    Qt Code:
    1. ...
    2.  
    3. t = new QTableWidget();
    4. t->setSelectionMode(QAbstractItemView::NoSelection);
    5. t->setColumnCount(2);
    6. t->setHorizontalHeaderLabels(labels);
    7. t->setItemDelegateForColumn(1, new ImageDelegate(this));
    8.  
    9. QTableWidgetItem *item[100][2];
    10.  
    11. for(int i=0;i < 100; i++)
    12. {
    13. t->insertRow(i);
    14.  
    15. item[i][0] = new QTableWidgetItem(s.arg(i));
    16. item[i][0]->setFlags(item[i][0]->flags() & ~Qt::ItemIsEditable);
    17. t->setItem(i, 0, item[i][0]);
    18.  
    19. item[i][1] = new QTableWidgetItem(":/icons/unknown.png");
    20. item[i][1]->setData(Qt::UserRole, ":/icons/unknown.png");
    21. item[i][1]->setFlags(item[i][1]->flags() & ~Qt::ItemIsEditable);
    22. t->setItem(i, 1, item[i][1]);
    23.  
    24. //i tried a bunch of stuff... resizeColumnToContents() and resizeRowToContents() works,
    25. //but only without any parameters and I guess it's not very good to recalculate everything
    26. //everytime I add something ? I'd like to just resize the inserted row AND that the already
    27. //inserted row keep their size.
    28.  
    29. //t->resizeColumnToContents(0);
    30. //t->resizeColumnToContents(1);
    31. //t->resizeRowToContents(i);
    32. //t->resizeColumnsToContents();
    33. //t->resizeRowsToContents();
    34. //t->setColumnWidth(1,140);
    35. //t->setRowHeight(i,200);
    36.  
    37. //t->scrollToBottom();
    38. }
    To copy to clipboard, switch view to plain text mode 

    imagedelegate.cpp:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "imagedelegate.h"
    4.  
    5. ImageDelegate::ImageDelegate(QObject *parent)
    6. {
    7. }
    8.  
    9. void ImageDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    10. const QModelIndex &index) const
    11. {
    12. QPixmap pixmap(index.model()->data(index).toString());
    13. painter->save();
    14. painter->drawPixmap(option.rect, pixmap);
    15. painter->restore();
    16. }
    17.  
    18. QSize ImageDelegate::sizeHint(const QStyleOptionViewItem & /* option */,
    19. const QModelIndex & /* index */) const
    20. {
    21. return(QSize(140,200));
    22. }
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: tablewidget and pixmap

    Quote Originally Posted by magoo View Post
    Another problem I have, why do I have to call resizeColumnsToContents() and resizeRowsToContents() if I add more than one rows to my TableWidget ? The cells size seems to reset after I add new TableWidgetItem to the table... There's probably a way for the table to remember the size of the cells that are already in the table...
    Provide a correct size hint in the delegate. For example:

    Qt Code:
    1. QSize myDelegate::sizeHint(const QStyleOptionViewItem & /* option */,
    2. const QModelIndex & index) const {
    3. return index.data(Qt::DecorationRole).toPixmap().size()+QSize(4,4);
    4. }
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Nov 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: tablewidget and pixmap

    I've put a qDebug() inside the sizeHint() method of the delegate and it's never actually called.

    I've try adding
    Qt Code:
    1. item[i][1]->setData(Qt::SizeHintRole, QSize(140,200));
    To copy to clipboard, switch view to plain text mode 
    in my mainwindow.cpp but the cells are still sizing incorrectly. if I call the sizeHint() method of the TableWidgetItem, it returns the correct value but the delegate sizeHint() is never call with or without adding the SizeHintRole data.

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: tablewidget and pixmap

    Try subclassing QItemDelegate instead of the abstract delegate.

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.