Results 1 to 3 of 3

Thread: How to get Icons in QTableWidgetItem to take up the whole item space

  1. #1
    Join Date
    Sep 2009
    Posts
    64

    Default How to get Icons in QTableWidgetItem to take up the whole item space

    Posted something about this earlier... but it kinda got lost in the posts... so im posing the question again with a better title haha.

    i have written a custom item delegate... but it doesnt seem to work

    Qt Code:
    1. class MyItemDelegate: public QItemDelegate
    2. {
    3. public:
    4. MyItemDelegate(QObject* pParent = 0) : QItemDelegate(pParent)
    5. {
    6. }
    7. void MyItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    8. {
    9. QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
    10. drawBackground(painter, option, index);
    11. drawDecoration(painter, option, option.rect, icon.pixmap(option.rect.size()));
    12. drawFocus(painter, option, option.rect);
    13. }
    14. };
    To copy to clipboard, switch view to plain text mode 

    anyone have any ideas? im real desperate to get this thing working, ive been working on it for days now and i dont know where else to look

  2. #2
    Join Date
    Sep 2009
    Posts
    64

    Default Re: How to get Icons in QTableWidgetItem to take up the whole item space

    seriously though... anyone see anything obvious that i am not doing? im going insane over here... its like im taking crazy pills!

  3. #3
    Join Date
    Sep 2009
    Posts
    64

    Default Re: How to get Icons in QTableWidgetItem to take up the whole item space

    got something to work... not perfect, but the icon will now keep aspect ration in the item. enjoy!!!

    Qt Code:
    1. #include <QStyledItemDelegate>
    2. #include <QPainter>
    3.  
    4. class ItemDelegate : public QStyledItemDelegate
    5. {
    6. public:
    7. enum ItemDataRole { SubTextRole = Qt::UserRole + 100 };
    8.  
    9. ItemDelegate(QObject *parent = 0) : QStyledItemDelegate(parent) {}
    10. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
    11. void paint(QPainter *p, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    12. };
    13.  
    14. QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    15. {
    16. QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
    17. QString line1 = index.data(Qt::DisplayRole).toString();
    18. //QString line2 = index.data(SubTextRole).toString();
    19.  
    20. int textW = option.fontMetrics.width(line1);
    21. QSize iconSize = icon.actualSize(option.decorationSize);
    22.  
    23. return QSize(iconSize.width() + 4,
    24. iconSize.height() + 2 + option.fontMetrics.lineSpacing() * 2 + 4);
    25. }
    26.  
    27. void ItemDelegate::paint(QPainter *p, const QStyleOptionViewItem &option, const QModelIndex &index) const
    28. {
    29. p->save();
    30.  
    31. QString line1 = index.data(Qt::DisplayRole).toString();
    32. QString line2 = index.data(SubTextRole).toString();
    33.  
    34. QStyleOptionViewItemV4 opt(option);
    35. initStyleOption(&opt, index);
    36.  
    37. QStyle *style = opt.widget->style();
    38. style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, p, opt.widget);
    39.  
    40. if (option.state & QStyle::State_Selected)
    41. p->setPen(QPen(option.palette.brush(QPalette::HighlightedText), 0));
    42.  
    43. QRect itemRect = option.rect.adjusted(2, 2, -2, -2);
    44. QRect r = QStyle::alignedRect(opt.direction, Qt::AlignHCenter | Qt::AlignLeft, itemRect.size(), itemRect);
    45. opt.icon.paint(p, r);
    46.  
    47. int h = option.fontMetrics.lineSpacing();
    48. QRect textRect(itemRect.left(), itemRect.bottom() - h, itemRect.width(), h);
    49.  
    50. p->drawText(textRect, Qt::AlignVCenter | Qt::AlignHCenter, line1);
    51.  
    52. QColor subTextColor = p->pen().color();
    53. subTextColor.setAlphaF(.5);
    54.  
    55. p->restore();
    56. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Two icons for each item in QMenu?
    By ultr in forum Qt Programming
    Replies: 9
    Last Post: 15th December 2010, 15:13
  2. QTableView empty space or too little space.
    By davemar in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2009, 16:00
  3. Replies: 1
    Last Post: 26th July 2009, 15:08
  4. Replies: 14
    Last Post: 9th November 2006, 08:35
  5. linking user space and kernel space programs with qmake
    By zielchri in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2006, 23:11

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.