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 Customize QListWidgetItem, how to?

    I'm using QListWidget to list things in my application. It's very easy to use but also very simple. What if I want a custom design of my QListWidgetItem? The standard is just one row of text but what if I want one large label and other smaller labels that shows text on my QListWidgetItem? Is there an easy way to cusomize this or should I use some other widget for this? Also is there possible to add Buttons on the QListWidgetItem?

    A list like this is something I would like:


    Thanks really much for your help!

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

    hisong1988 (25th December 2010), Lawand (22nd February 2011), steveyzhang (4th February 2011)

  3. #2
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize QListWidgetItem, how to?

    i think iis more simple use a QListView whit a custom delegate.
    Last edited by ecanela; 2nd February 2010 at 18:52. Reason: updated contents

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

    Default Re: Customize QListWidgetItem, how to?

    Hi and thanks!

    Is there any examples on how to use a custom delegate for a QListView?

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Customize QListWidgetItem, how to?

    Sure, in the 'examples' directory of the Qt distribution

  6. #5
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Customize QListWidgetItem, how to?

    Also check the ICS Network video tutorials at : http://www.ics.com/learning/icsnetwork/
    Look for the model-view framework video.

  7. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize QListWidgetItem, how to?

    Its not necessary to use QListView for delegates. You can use delgates with QListWidget.
    Check the spin box and star delegate examples in Qt Demo.

  8. The following user says thank you to aamer4yu for this useful post:

    martinn (5th February 2010)

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

    Default Re: Customize QListWidgetItem, how to?

    Thanks for your help!

    I found out that I also could use QListWidget::setItemWidget(), which seems simplier than using a delegate?

    This is what I tried, I can now build the ui of MyItem using the Design Editor:

    Qt Code:
    1. //MyItem is a custom widget that takes two strings and sets two labels to those string.
    2. MyItem *myItem = new MyItem("Text for label1","Text for label2");
    3. item->setSizeHint(QSize(0,65));
    4. ui.listWidget->addItem(item);
    5. ui.listWidget->setItemWidget(item,myItem);
    To copy to clipboard, switch view to plain text mode 

    What is the best way if all I want is to show two different labels and an image, using a delegate or setItemWidget() like above? Is the way I use above a "heavy" solution?

    If I use a delegate can I create my ListItem using Designer Editor then? And if so, where can I find examples of this?

    Thanks again!
    Last edited by martinn; 3rd February 2010 at 10:46.

  10. The following 3 users say thank you to martinn for this useful post:

    Astronomy (3rd February 2010), JandunCN (30th December 2013), San_Cou (17th January 2013)

  11. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize QListWidgetItem, how to?

    Is the way I use above a "heavy" solution?
    Yes

    If you use delegate, you can create your item in designer, but you cant see the effects of the delegate from the designer. You will need to run the code and check

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

    Default Re: Customize QListWidgetItem, how to?

    Thanks! I thought it seemed a little heavy..

    Is there any examples of using the designer when creating the item? (Sorry but I'm a real newbie to Qt)

  13. #10
    Join Date
    May 2009
    Location
    Vienna
    Posts
    91
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize QListWidgetItem, how to?

    Hi,
    Thanks to all for these General ideas, i can use them for my similar problem too.

    greetz Astronomy

  14. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Customize QListWidgetItem, how to?

    In the designer, double click on the QListWidget, you will get a dialog to add items.
    Did you try it yet ?

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

    Default Re: Customize QListWidgetItem, how to?

    Quote Originally Posted by aamer4yu View Post
    In the designer, double click on the QListWidget, you will get a dialog to add items.
    Did you try it yet ?
    Oh sorry! What I meant was if I could use the designer to build the item in the delegate. Right now I'm using the code below to create the delegate with QPainter as you can see. But I would like it if it was possible to create my ui in the designer instead and add my labels and so on.

    Is that possible? And is there any examples of this?

    Qt Code:
    1. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    2. QColor background;
    3.  
    4. if (option.state & QStyle::State_Selected) {
    5. background = Qt::darkGray;
    6. } else {
    7. background = Qt::lightGray;
    8. }
    9. painter->fillRect(....
    10. ....
    11. //More code here
    12. ....
    13. painter->drawText(option.rect, Qt::AlignCenter, index.model()->data(index).toString());
    14. }
    To copy to clipboard, switch view to plain text mode 

  16. #13
    Join Date
    Dec 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Customize QListWidgetItem, how to?

    Can u send this sorce code for me.

  17. #14
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Customize QListWidgetItem, how to?

    thank you very much, very useful thread...

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.