Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can create QTreeView with different Objects
QtWS25 Last Chance

How can create QTreeView with different Objects

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kiko007
    wrote on last edited by
    #1

    This is my doubt, I have 4 classes, Institution, College, Careers and Department. Each one of them has a name and other attributes, but what interests me is the name only. I want to show in a QTreeView and therefore make a subclassing of QAbstractItemModel, this is where my doubt, trying to reimplement the index, parent and rowCount functions. I do it with class Institution, but with the other hierarchies I lose, I really do not know how to refer to them.

    This is the code of the classes, how to reimplement the index, parent and rowCount functions?

    @Class Institution {

    public:
    Institution(const QString aName);

    QSting name;
    QList<College*> child;
    };

    Class College {

    public:
    // Here I initialize the name and parent
    College(const QString aName, Institution *aParent = 0);

    QSting name;
    QList<Careers*> child;
    Institution *parent;
    };

    Class Careers {

    public:
    // Here I initialize the name and parent
    Careers(const QString aName, College *aParent = 0);

    QSting name;
    QList<Department*> child;
    College *parent;
    };

    Class Department {

    public:
    // Here I initialize the name and parent
    Department(const QString aName, Careers *aParent = 0);

    QSting name;
    Careers *parent;
    };
    @

    Help me, please!
    Thanks you

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      The information you gave is far too vague, we don't know what you want to do.

      There are some tutorials and examples in the docs and the wiki:

      • "Item Views Examples ":http://developer.qt.nokia.com/doc/qt-4.7/examples-itemviews.html from the docs
      • "Guide to Model/View programming":http://developer.qt.nokia.com/doc/qt-4.7/model-view-programming.html of the docs
      • various forum threads

      I'd advice you to start with one of them.

      Four your design of the different objects:
      You could create a common base class that defines the name() method. In your model storage class use only this base class.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Another approach is to create a struct like this:

        @
        struct TreeNode {
        enum TreeNodeType {
        Institution,
        College,
        Career,
        Department
        };

        TreeNodeType type;
        void* object;
        QList<TreeNode> childNodes;
        }
        @

        Instead of a void pointer and a type flag (I know, not very type save), you can of course also use other solutions like having a pointer to each of the four different classes in there (and have only one be non-0) or even to have a base TreeNode class, and make four subclasses from it, one for each of the types you need to store.

        Your index pointer in your QModelIndex will point to the TreeNode class, not to your instances directly. That way, you can be sure what to case your pointer to. Again, an alternative for that is to use a single (flat) list of nodes in the background, and put the index in this list as the item data in your QModelIndex.

        However you do it: tree models stay hard to get right, and the way QAIM and QModelIndex are constructed does not make it easier to create mixed-type trees.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sorevi6715
          Banned
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved