PDA

View Full Version : QTreeWidget and styles



arturo182
27th August 2009, 13:23
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):

http://img386.imageshack.us/img386/1990/clipboard04.png

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.

victor.fernandez
27th August 2009, 13:31
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() (http://qt.nokia.com/doc/4.5/qabstractitemmodel.html#rowCount), columnCount() (http://qt.nokia.com/doc/4.5/qabstractitemmodel.html#columnCount), headerData() (http://qt.nokia.com/doc/4.5/qabstractitemmodel.html#headerData) and data() (http://qt.nokia.com/doc/4.5/qabstractitemmodel.html#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.

arturo182
27th August 2009, 13:32
Could you maybe provide more info about doing this or some sample code.

arturo182
29th August 2009, 14:40
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.

aamer4yu
30th August 2009, 15:22
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 (http://doc.trolltech.com/qq/qq24-delegates.html) might be useful to you.

arturo182
30th August 2009, 15:32
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.