0

I want to build a tree with items which are always automatically sorted under a tree node when adding, renaming, and doing other operation. std::set seems a good candidates for my data container. However, it seems Qt tree favors vectors or QList (a pointer vector) since the tree items are accessed, inserted, or removed via their indices or row numbers. I am using std::distance like function to calculate index for an item in the set. But I think it is very slow (not tested). Any good way to let Qt tree model work with std::set, or may I need to use vectors for my data, or develop a new container to do it? Thanks a lot!

Find an answer: use boost::container::flat_set. Thanks.

user1899020
  • 13,167
  • 21
  • 79
  • 154

2 Answers2

2

QSet is not a good idea for this purpose. As well as not providing index-based access, it's completely unordered, that is there are absolutely no guarantees on the order of traversal. There's no sensible way that can be made to work with a Qt item model. You'd be much better off just using QList and ensuring the values are unique yourself.

Dan Milburn
  • 5,600
  • 1
  • 25
  • 18
  • I had thought QSet is like std::set. But it is not. Thank you for your notes. – user1899020 Jan 02 '13 at 18:50
  • @user1899020: But even `std::set` doesn't support indexes right? It doesn't seem to be much different of a situation other than offering a "weak" sorting. – jdi Jan 02 '13 at 18:59
0

You should probably use QTreeView , and subclass QAbstractItemModel this way you can use any data source you want

see this question : Creating Qt models for tree views

Community
  • 1
  • 1
Youssef Bouhjira
  • 1,599
  • 2
  • 22
  • 38