PDA

View Full Version : Programming technique with QTreeWidget or QListWidget



adamb924
31st January 2011, 19:11
This is a question about programming practice. I am looking for opinions.

Not infrequently I write applications where users interact with data through a list or tree widget of some sort. I've sort of stumbled upon two approaches to this:

(1) I respond to events in the tree|list widget by figuring out the index of the widget item, and then modify the data structure (e.g. a QList) based on that widget.
(2) I subclass QTreeWidgetItem (or QListWidgetItem), and include a pointer to the appropriate data structure in the new class. Then when an event occurs, I cast the pointer to QTreeWidgetItem to the subclass, and modify the data structure that way.

At this point in my thinking, option #2 seems more elegant. I am open to suggestions.

I should mention that while I'm aware of model/view programming, I've always had fairly simple applications, and whenever I've looked closely it looked like more trouble than it was worth (for my particular applications). If you think that model/view is definitely the way to go in any circumstance, I'd be interested to hear that.

Thanks for your thoughts,
Adam

helloworld
31st January 2011, 21:53
Just to make sure if I understand correctly, are you using QListWidget/QTreeWidget? (edit: well, I guess you are, now when I read the title of your post) These are convenience classes that you can use without subclassing QAbstractItemModel. They are, btw part of the Model/View framework so in this case, you are already using this approach.

To me it sounds a bit like you are trying to re-implement existing functionality. Unless you have very specific requirements, I think that using built-in functionality as much as possible will give you a more flexible and reusable solution in the end.