Results 1 to 7 of 7

Thread: QTreeView details: what are viewItems?

  1. #1
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Wink QTreeView details: what are viewItems?

    I am working on understanding trees, working through the simpletreemodel example that comes with Qt. I am working in PySide, not Qt, and while I don't know much c++ I often find it helpful to look at the Qt source code to help me understand what is happening.

    I understand indexes a bit (because of a thread here), and now am trying to understand QTreeView, in particular how it paints the initial tree, determines what to draw, when/where it calls model.rowCount() and other methods from my QAbstractItemModel subclass.

    I've been looking over qtreeview.cpp and am confused. My first confusion is about what the 'd' class is. I think it is an instance of QTreeView, because starting on line 196 we have:
    Qt Code:
    1. QTreeView::QTreeView(QTreeViewPrivate &dd, QWidget *parent)
    2. : QAbstractItemView(dd, parent)
    3. {
    4. Q_D(QTreeView);
    5. d->initialize();
    6. }
    To copy to clipboard, switch view to plain text mode 
    But there are lots of methods/attributes of d that I cannot find defined anywhere. For instance viewItems seems pretty key, and has many methods (e.g., resize(), clear()) that I'd like to study. Unfortunately I am not sure where viewItems is defined initially, what it means. I have never seen it documented, or used it explicitly in my custom views. In addition to qtreeview.cpp, I searched for it in qtreeview.h, qabstractitemview.cpp, and qabstractitemview.h.

    So I seem to be having a problem similar to when I was tracking down the definition of createIndex. Namely, figuring out where to find the relevant source code.

    More generally, anyone please let us know if you have seen a good nuts 'n' bolts explanation of how QTreeView works. I have found high-level descriptions of what is going on (e.g., the view uses indexes to determine what to paint to the screen), but I really want to know more under the hood. I have started putting print commands in the model for all the methods (e.g., rowCount), but this has made me even more confused: for instance, sometimes it is calling rowCount with input from a parent index from a row that is way below my selection.

    Thanks for reading.
    Last edited by neuronet; 1st January 2015 at 06:35.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView details: what are viewItems?

    The d-pointer idiom is used to keep the public API of QTreeView binary stable while still having the option of adding/removing/renaming functions necessary for the view's procesing.

    The d-pointer points to a private class that holds the actual implementation of the view, often also referred to as PIMPL idiom (pointer to implementation).

    The Q_D() and Q_Q() macros enable const correct access to the private and public class respectively.

    The class you are looking for ist QTreeViewPrivate

    Cheers,
    _

  3. #3
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QTreeView details: what are viewItems?

    Quote Originally Posted by anda_skoa View Post
    The d-pointer idiom is used to keep the public API of QTreeView binary stable while still having the option of adding/removing/renaming functions necessary for the view's procesing.

    The d-pointer points to a private class that holds the actual implementation of the view, often also referred to as PIMPL idiom (pointer to implementation).

    The Q_D() and Q_Q() macros enable const correct access to the private and public class respectively.

    The class you are looking for ist QTreeViewPrivate

    Cheers,
    _
    Thanks I will ruck about see what I figure out and post here.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTreeView details: what are viewItems?

    Within the Qt source the private implementation is usually in a file named blah_p.h for a class blah with its public API in blah.h. The private header is #included into the implementation file blah.cpp but not inthe public header or user programs.

    I would not worry too much about the internals of the views and models if all you want to do is use them.

  5. The following user says thank you to ChrisW67 for this useful post:

    neuronet (1st January 2015)

  6. #5
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QTreeView details: what are viewItems?

    Quote Originally Posted by ChrisW67 View Post
    Within the Qt source the private implementation is usually in a file named blah_p.h for a class blah with its public API in blah.h. The private header is #included into the implementation file blah.cpp but not inthe public header or user programs.

    I would not worry too much about the internals of the views and models if all you want to do is use them.
    Thanks a lot for the specifics, that is helpful, though I am frankly still a bit lost.

    tl;dr
    : Good point: my goal is just to use this software, not become a developer. If it really is important I learn the internals, I will need to learn Qt for real (c++).

    The problem is, as I try to explain to other PySide users what is going on underneath, I often have no idea. For instance "Why the redundancy between model.data() and index.data(): why not always use one or the other?" It is frustrating to not be able to go in and figure such things out easily on my own. c++ code is a labyrinthine mystery to me for the most part. For some things, going to the source is really helpful. With QTreeView, it has been mostly frustrating.

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView details: what are viewItems?

    QModelIndex::data() is for convenience, in case you want to retrieve data pointed to by a QModelIndex.

    But the data retrieval function has to be implemented somewhere and since one cannot "inject" something into QModelIndex, the next possible place is the model.

    So QModelIndex::data() can then always be just a simple indirection
    Qt Code:
    1. QVariant QModelIndex::data(int role) const
    2. {
    3. model()->data(*this, role),
    4. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    neuronet (2nd January 2015)

  9. #7
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QTreeView details: what are viewItems?

    I found a great Doxygen resource for navigating Qt (unfortunately only up to 4.2 I couldn't find any doxygenated 4.8: if anyone has a link let me know...).

    Using it made navigating the c++ code much easier, and the code seems largely the same as in 4.8, so I've been using the doxygen pages (for 4.2) to find stuff in 4.8. For instance, the documentation for qtreeview_p.h has the following useful bit:
    QVector<QTreeViewItem> QTreeViewPrivate::viewItems [mutable]
    Definition at line 134 of file qtreeview_p.h.
    Cool, we have a QVector of QTreeViewItems, and this is the mysterious viewItems that I was asking about! Many of the viewItems methods were actually just QVector methods, and part of my confusion is that QVector is not part of PySide.

    At any rate, in 4.8, on line 178), we see the relevant line:
    Qt Code:
    1. mutable QVector<QTreeViewItem> viewItems;
    To copy to clipboard, switch view to plain text mode 
    So we have a vector of QTreeViewItem types, and QTreeViewItem has its own page here. But more importantly, qtreeviewitem is the first structure defined in qtreeview_p.h! In terms of clues about when it is being populated, check line 2495 of qtreeview.cpp (d->viewItems.insert(insertPos, delta, insertedItems.at(0))). Where 'insert' seems to be a built-in method for QVectors.

    This seems like a very promising line of attack for other Python users (perhaps future me) to insinuate themselves into this often baffling code.

Similar Threads

  1. QTreeView
    By marnando in forum Newbie
    Replies: 1
    Last Post: 13th March 2014, 09:06
  2. QTreeView and d&d
    By FuturePrimitive in forum Qt Programming
    Replies: 1
    Last Post: 10th November 2011, 21:27
  3. Help ...QTreeView
    By Raymond in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2009, 10:18
  4. QTreeView help
    By bepaald in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 22:22
  5. QTreeView
    By npc in forum Qt Programming
    Replies: 2
    Last Post: 24th January 2007, 08:53

Tags for this Thread

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.