You are not logged in.

1

Friday, February 27th 2004, 4:43pm

Backing QListsBoxItems with Objects?

The examples I've seen of QListBox all use a QString as the data displayed in a text QListBoxItem. I'm aware of another programming language which provides the ability to create a similar widget using a variety of objects. Usually a 'toString()' method is called on the object used as an item in the list in order to get the display text. Is that possible in Qt without a lot of trouble? As an example, suppose I had a DOM NodeList of Element nodes. I want to use that directly to populate the list, perhaps with some kind of wrapper class that determines how the Element is represented as a string?

Should I simply subclass QListBoxItem somehow to turn /it/ into a wrapper class? Am I likely to brake stuff related to Qt's memory cleanup that way?

e8johan

Professional

  • "e8johan" is male

Posts: 1,195

Location: Sweden

  • Send private message

2

Friday, February 27th 2004, 6:05pm

RE: Backing QListsBoxItems with Objects?

Quoted

Originally posted by Hetware
Should I simply subclass QListBoxItem somehow to turn /it/ into a wrapper class? Am I likely to brake stuff related to Qt's memory cleanup that way?


Yes, subclassing is the way to go. And, no, you will not break Qt's memory cleanup unless you do something strange.

3

Sunday, February 29th 2004, 4:36pm

RE: Backing QListsBoxItems with Objects?

Quoted

Originally posted by e8johan

Yes, subclassing is the way to go. And, no, you will not break Qt's memory cleanup unless you do something strange.


OK, so the obvious next step is to rearrange the list, or add to, or delete from the list. Do I also subclass QListBox in such a way that items are displayed in the order of the backing the list?

This seems like a very generic design pattern. I wonder if support for this might be part of a future Qt release. Is the sort of thing done by other Qt programmers frequently?

e8johan

Professional

  • "e8johan" is male

Posts: 1,195

Location: Sweden

  • Send private message

4

Sunday, February 29th 2004, 7:27pm

RE: Backing QListsBoxItems with Objects?

Quoted

Originally posted by Hetware

Quoted

Originally posted by e8johan

Yes, subclassing is the way to go. And, no, you will not break Qt's memory cleanup unless you do something strange.


OK, so the obvious next step is to rearrange the list, or add to, or delete from the list. Do I also subclass QListBox in such a way that items are displayed in the order of the backing the list?

This seems like a very generic design pattern. I wonder if support for this might be part of a future Qt release. Is the sort of thing done by other Qt programmers frequently?


I'd go for a solution where you move the listbox-items when you move list items, this avoids sub-classing QListBox.

I ran into this when writing the Independent Qt Tutorial, chapter 7 (http://www.digitalfanatics.org/projects/…/chapter07.html) where I use a "synced" approach. Perhaps a better way to do it would have been to keep a reference from the backing-store to each listbox-item. This simplifies deletion and modification, the only case left then is movement.

This post has been edited 1 times, last edit by "e8johan" (Mar 1st 2004, 7:50am)