Results 1 to 11 of 11

Thread: QTreeView with Model-View help

  1. #1
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTreeView with Model-View help

    Hi,

    I am trying to make a tree for my data objects, but I am having trouble figuring out how I should approach it to make it as clean as possible. I was using a QTreeWidget, but it felt very hackish and wasn't very nice to keep up with. I'd like to use a QTreeView, but I am unsure of how to handle the models. An example of how my data classes work is below.

    • data class 1
      • group
        • sub-data class 1
          • group
            • sub-sub-data class 1
      • group
      • group
    • data class 2
      • group
      • group
      • group


    At the root, there are the large data classes, then there are groups of related data classes within that (the groups keep unrelated data classes from grouping together). When a data class is added, it needs to build its subtree of data and groups. the group "labels" cant be selected, but the data classes each have a widget for editing the internal data. How should I design the model to handle this?

    --MathStuf

    Edit: Typoed QList* rather than QTree*
    Last edited by MathStuf; 21st April 2008 at 20:00.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView with Model-View help

    Shouldn't you be using QTreeView and not QListView?

  3. #3
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView with Model-View help

    Wow, that's a typo. I'll edit to fix that.

    --MathStuf

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView with Model-View help

    Quote Originally Posted by MathStuf View Post
    How should I design the model to handle this?
    Could you explain what do you mean by the word "how" here? The question is very general, so it's hard to provide a detailed answer to your question.

  5. #5
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView with Model-View help

    I would like to know how I could got the model to handle the different types of data classes without being a huge if else tree. If that's the only way, I'll make it work, but I was wondering if something like template or subclassing could work here. I'm not sure how the internals of the model-view work and if that would work at all.

    --MathStuf

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView with Model-View help

    I'd go for subclassing. Make your elements derived from a common base class with virtual methods and delegate the model's methods to items using virtual methods.

  7. #7
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView with Model-View help

    All of my data classes are derived from a common base class. Not all of the data classes have the same getter/setter methods, so there are different widgets for each, again which are derived from a common base class.

    If you mean subclassing the models for each data class , how would i get the QTreeView to use a different model depending on the item type? If I'm misinterpreting what you're saying, let me know.

    I think one of the places I keep tripping over is how to figure out is how to keep the grouping entries from getting confused with the data entries. I wish that this would just straighten itself out in my mind like the rest of the "how should I do this?" problems have with this project.

    --Ben

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView with Model-View help

    I mean exactly what I wrote Provide a common interface for all types of objects your tree carries and use the interface from within the model. For instance:
    Qt Code:
    1. class Base {
    2. public:
    3. QVariant data(int role=Qt::DisplayRole) const = 0;
    4. };
    5.  
    6. class Level1Item : public Base {
    7. public:
    8. QVariant data(int role=Qt::DisplayRole) const {
    9. return myName;
    10. }
    11. };
    12.  
    13. QVariant Model::data(const QModelIndex &index, int role){
    14. if(!index.isValid()) return QVariant();
    15. Base *ptr = static_cast<Base*>(index.internalPointer());
    16. return ptr->data(role);
    17. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView with Model-View help

    Oh, ok. I see what you mean now. Thanks a lot.

    --MathStuf

  10. #10
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView with Model-View help

    I have another question with this. Each model can change the number of children rows from within (i.e. the Level1Item class above) with setData. Do I have to signal beginRemoveRows/beginInsertRows and their end equivalent?

    --MathStuf

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView with Model-View help

    Yes, you have to emit some signals. Emitting layoutChanged() or even reset() might be an option instead of using begin/end*Rows.

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Help with Model View
    By weepdoo in forum Qt Programming
    Replies: 13
    Last Post: 12th October 2007, 10:32
  3. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  4. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 13:01
  5. Replies: 6
    Last Post: 20th April 2006, 10:23

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.