Results 1 to 6 of 6

Thread: QTreeWidget and styles

  1. #1
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows Symbian S60
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Default QTreeWidget and styles

    Is it possible to style just the chosen level items of QTreeWidget, like apply different styles to top level items, level 1 items, etc.

    I guess there's no need for me to create a second topic so I'll ask here: how o I use QTreeView? I know I have to use models and I read the documentation and analised the examples but I still don't get it. For example if I wanted to make a contact list for a IM and I want it to look like this (mock up):



    I tried custom painting it trough QTreeWidget but that takes a lot of code and CPU when redrawing. I'm sure there's some easy way to do it, I mean - it's Qt.

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 24 Times in 24 Posts

    Default Re: QTreeWidget and styles

    First, implement a model that provides the data for the view. For a view like the one you want, you may subclass QAbstractListModel and reimplement rowCount(), columnCount(), headerData() and data().For instance, data() might return the icon while asked for the Qt::DecorationRole, and with Qt::DisplayRole on column 0 the name and on column 1 the message.

    Then you will need to implement a custom delegate in order to achieve that look. The custom delegate may paint what you want directly.

    Another possibility is to make the model return HTML code and in your custom delegate use QTextDocument::drawContents() to render it and paint it.
    Last edited by victor.fernandez; 27th August 2009 at 13:57.

  3. The following user says thank you to victor.fernandez for this useful post:

    arturo182 (30th August 2009)

  4. #3
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows Symbian S60
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Default Re: QTreeWidget and styles

    Could you maybe provide more info about doing this or some sample code.

  5. #4
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows Symbian S60
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Default Re: QTreeWidget and styles

    I've been reading Qt help about using models and delegates since yesterday and I still don't get it, it's really messed up. I tried a lot of approaches and can't even start getting it right. Could someone please explain me how models and delegates work. All I need is a simple example to get me started. For example how do I achieve a QTreeView that looks like the picture I provided in my first post. Is it really that hard to do or am I just that stupid?

    You don't even have to write me example code, just explain what to do, please.

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

    Default Re: QTreeWidget and styles

    Is it possible to style just the chosen level items of QTreeWidget, like apply different styles to top level items, level 1 items, etc.
    Using delegates, you get QModelIndex, based on which you can choose how to paint a row.

    You may need not go down to using model and view, you can very well use delegates with QTreeWidget. You will need either QItemDelegate or QStyledItemDelegate(from 4.5 onwards). In the paint event you can draw your contents. You will get the rect area in option.rect.

    As for examples, you can find delegates example in Qt Demo under ItemViews section.
    Also this example from Qt quarterly might be useful to you.

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

    arturo182 (30th August 2009)

  8. #6
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows Symbian S60
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Default Re: QTreeWidget and styles

    After a lot of reading and analyzing examples I finally managed to create my own model and delegate. It was way too hard to get to do it. Qt documentation isn't the best in this topic. Or maybe it's just me.
    Either way I got my answers so thanks, I think I can handle things from this point.

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.