Results 1 to 20 of 25

Thread: Customize QListWidgetItem, how to?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: Customize QListWidgetItem, how to?

    Thanks for your answer! I thought that it was possible to use the stylesheets just like I use to even if I was using a delegate and painted everything. My misstake.. So if I want gradient (like the picture in my first post) I should just paint the gradient. And the stylesheets can't be used at all here?

  2. #2
    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: Customize QListWidgetItem, how to?

    Read documentation about style sheet and check theirs functionality. If you decide that all you want to have in your view can be done with style sheet, then you can just use them without implementing your custom delegate at all. But if you decide that style sheet functionality is not enough for you then I suggest to do all your custom look in custom delegate as it gives you much more ways to customize your view.
    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.

  3. #3
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: Customize QListWidgetItem, how to?

    Now I got my delegate up and running and I was able to paint it so it looks like I want. Thanks!

    One thing is that I'm trying to add an image to each row as you can see in the code below. This is really slow.. Is it always slow when adding an image for each row or am I doing something wrong in my code? Is there a better way of adding the image?

    This is the code where I load my listWidget:
    Qt Code:
    1. for (int i = 0; i < 50; i++) {
    2. item->setData(Qt::DisplayRole, "This is the title");
    3. item->setData(Qt::UserRole + 1, "This is description");
    4. item->setData(Qt::DecorationRole, QPixmap(":/images/myimage.png"));
    5. listWidget.addItem(item);
    6. }
    To copy to clipboard, switch view to plain text mode 

    In my delegate I paint the image like this:
    Qt Code:
    1. QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
    2. r = option.rect;
    3. ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2010
    Posts
    27
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Customize QListWidgetItem, how to?

    hi martinn,

    I am looking to implement a very similar type of list and I an wondering if it would be possible for you to share your code?

    many thanks,

    Dubstar_04

  5. #5
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: Customize QListWidgetItem, how to?

    To set the delegate for a list:
    Qt Code:
    1. myListWidget->setItemDelegate(new ListDelegate(myListWidget));
    2. item->setData(Qt::DisplayRole, "Title");
    3. item->setData(Qt::UserRole + 1, "Description");
    4. myListWidget->addItem(item);
    To copy to clipboard, switch view to plain text mode 

    ListDelegate.h:
    Qt Code:
    1. #include <QPainter>
    2. #include <QAbstractItemDelegate>
    3.  
    4. class ListDelegate : public QAbstractItemDelegate
    5. {
    6. public:
    7. ListDelegate(QObject *parent = 0);
    8.  
    9. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    10. QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    11.  
    12. virtual ~ListDelegate();
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 

    ListDelegate.cpp:
    Qt Code:
    1. #include "ListDelegate.h"
    2.  
    3. ListDelegate::ListDelegate(QObject *parent)
    4. {
    5.  
    6. }
    7.  
    8. void ListDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
    9. QRect r = option.rect;
    10.  
    11. //Color: #C4C4C4
    12. QPen linePen(QColor::fromRgb(211,211,211), 1, Qt::SolidLine);
    13.  
    14. //Color: #005A83
    15. QPen lineMarkedPen(QColor::fromRgb(0,90,131), 1, Qt::SolidLine);
    16.  
    17. //Color: #333
    18. QPen fontPen(QColor::fromRgb(51,51,51), 1, Qt::SolidLine);
    19.  
    20. //Color: #fff
    21. QPen fontMarkedPen(Qt::white, 1, Qt::SolidLine);
    22.  
    23. if(option.state & QStyle::State_Selected){
    24. QLinearGradient gradientSelected(r.left(),r.top(),r.left(),r.height()+r.top());
    25. gradientSelected.setColorAt(0.0, QColor::fromRgb(119,213,247));
    26. gradientSelected.setColorAt(0.9, QColor::fromRgb(27,134,183));
    27. gradientSelected.setColorAt(1.0, QColor::fromRgb(0,120,174));
    28. painter->setBrush(gradientSelected);
    29. painter->drawRect(r);
    30.  
    31. //BORDER
    32. painter->setPen(lineMarkedPen);
    33. painter->drawLine(r.topLeft(),r.topRight());
    34. painter->drawLine(r.topRight(),r.bottomRight());
    35. painter->drawLine(r.bottomLeft(),r.bottomRight());
    36. painter->drawLine(r.topLeft(),r.bottomLeft());
    37.  
    38. painter->setPen(fontMarkedPen);
    39.  
    40. } else {
    41. //BACKGROUND
    42. //ALTERNATING COLORS
    43. painter->setBrush( (index.row() % 2) ? Qt::white : QColor(252,252,252) );
    44. painter->drawRect(r);
    45.  
    46. //BORDER
    47. painter->setPen(linePen);
    48. painter->drawLine(r.topLeft(),r.topRight());
    49. painter->drawLine(r.topRight(),r.bottomRight());
    50. painter->drawLine(r.bottomLeft(),r.bottomRight());
    51. painter->drawLine(r.topLeft(),r.bottomLeft());
    52.  
    53. painter->setPen(fontPen);
    54. }
    55.  
    56. //GET TITLE, DESCRIPTION AND ICON
    57. QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
    58. QString title = index.data(Qt::DisplayRole).toString();
    59. QString description = index.data(Qt::UserRole + 1).toString();
    60.  
    61. int imageSpace = 10;
    62. if (!ic.isNull()) {
    63. //ICON
    64. r = option.rect.adjusted(5, 10, -10, -10);
    65. ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
    66. imageSpace = 55;
    67. }
    68.  
    69. //TITLE
    70. r = option.rect.adjusted(imageSpace, 0, -10, -30);
    71. painter->setFont( QFont( "Lucida Grande", 6, QFont::Normal ) );
    72. painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft, title, &r);
    73.  
    74. //DESCRIPTION
    75. r = option.rect.adjusted(imageSpace, 30, -10, 0);
    76. painter->setFont( QFont( "Lucida Grande", 5, QFont::Normal ) );
    77. painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignLeft, description, &r);
    78. }
    79.  
    80. QSize ListDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const{
    81. return QSize(200, 60); // very dumb value
    82. }
    83.  
    84. ListDelegate::~ListDelegate()
    85. {
    86.  
    87. }
    To copy to clipboard, switch view to plain text mode 

  6. The following 7 users say thank you to martinn for this useful post:

    Chromatix (16th September 2014), codeslicer (20th December 2010), fathur_jtk09 (2nd September 2011), m.anis (29th July 2011), sivambigai (7th October 2010), steveyzhang (4th February 2011), sunil304047 (21st December 2011)

  7. #6
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize QListWidgetItem, how to?

    can anybody add example how to insert image into ListDelegate.cpp ?

  8. #7
    Join Date
    Dec 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Customize QListWidgetItem, how to?

    Excellent solution.
    My only problem is when I have variable text length for each Description, I cannot set dynamic sizeHint, particularly the height will be different for each item Description. Please help.

    QSize ListDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
    {
    QSize(200, 60); // very dumb value ***************=> How to make this vary with each ITEM?
    }

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Customize QListWidgetItem, how to?

    You get the index to the cell's data.

    Cheers,
    _

Similar Threads

  1. Customize QFileDialog
    By Swankee in forum Newbie
    Replies: 10
    Last Post: 25th November 2009, 20:21
  2. How to customize the icon?
    By snowdream in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2009, 06:59
  3. How customize my .exe with a icon.
    By jiveaxe in forum Qt Programming
    Replies: 2
    Last Post: 30th December 2007, 18:30
  4. how can i customize a QMainWindow
    By wygggyxtf in forum Qt Programming
    Replies: 15
    Last Post: 23rd June 2007, 06:18
  5. How could i customize my QtTabWidget?
    By Rodrigo in forum Qt Programming
    Replies: 3
    Last Post: 29th May 2007, 14:30

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.