Results 1 to 10 of 10

Thread: Source code for createIndex() ?

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

    Default Source code for createIndex() ?

    I am learning to code in PySide (Qt in Python), and find it helpful to look at the C++ source code for things when I am confused.

    I am having trouble finding the implementation of createIndex(), which we use for QAbstractItemModel. It is a method under QtCore.QAbstractItemModel, so I expect to find it in qabstractitemmodel.cpp. Indeed I see the documentation therein, but not the actual code that implements it.

    So, where is QAbstractItemModel::createIndex()? I'm not a C++ programmer, so assume I'm missing something obvious.

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Source code for createIndex() ?

    Hello,

    I looked into Qt 4.7.3 sources and there createIndex() is defined as an inline method in the header file under src/corelib/kernel/qabstractitemmodel.h. I assume this did not change in more recent versions (Qt 5.x).

    Best regards
    ars

  3. The following user says thank you to ars for this useful post:

    neuronet (30th December 2014)

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

    Default Re: Source code for createIndex() ?

    Quote Originally Posted by ars View Post
    Hello,

    I looked into Qt 4.7.3 sources and there createIndex() is defined as an inline method in the header file under src/corelib/kernel/qabstractitemmodel.h. I assume this did not change in more recent versions (Qt 5.x).
    You are right, got it thanks. Here it is:
    Qt Code:
    1. inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, void *adata) const
    2. { return QModelIndex(arow, acolumn, adata, this); }
    To copy to clipboard, switch view to plain text mode 
    And then I wonder what this QModelIndex function is, and it is defined in the same file:
    Qt Code:
    1. inline QModelIndex() : r(-1), c(-1), p(0), m(0) {}
    2. inline QModelIndex(const QModelIndex &other)
    3. : r(other.r), c(other.c), p(other.p), m(other.m) {}
    To copy to clipboard, switch view to plain text mode 
    And I don't really understand what it is doing (can anywone explain? (e.g., p(other.p)--what is that?) ).

    Perhaps I should have been more clear about my goal: I am asking because the implementation of index within QAbstractTableModel is:
    Qt Code:
    1. QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent) const
    2. {
    3. return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
    4. }
    To copy to clipboard, switch view to plain text mode 
    And I am confused about the third input to createIndex: why is it just 0 when the corresponding item has an index? Why doesn't it return the data item using something like an internalPointer? That is, I am used to sending an actual data item as the third input to createIndex (as in the simpletreemodel example that comes with Qt). (In C++ I think you send a pointer as the third item, but even so why send the integer 0?).
    Last edited by neuronet; 26th December 2014 at 21:30.

  5. #4
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Source code for createIndex() ?

    Qt Code:
    1. inline QModelIndex() : r(-1), c(-1), p(0), m(0) {}
    To copy to clipboard, switch view to plain text mode 
    This is the default constructor for a QModelIndex creating an invalid index. The implementation initializes internal members r, c, p and m to the values shown in () above.
    Qt Code:
    1. inline QModelIndex(const QModelIndex &other)
    2. : r(other.r), c(other.c), p(other.p), m(other.m) {}
    To copy to clipboard, switch view to plain text mode 
    This is the copy constructor creating a new QModelIndex as a copy of the existing QModelIndex other. The new index gets initialized with the internal state of the other object.

    In
    Qt Code:
    1. QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent) const
    2. {
    3. return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
    4. }
    To copy to clipboard, switch view to plain text mode 
    method
    Qt Code:
    1. createIndex(int, int, int)
    To copy to clipboard, switch view to plain text mode 
    gets called with 3rd parameter set to 0. In the implementation of this method you will find a reinterpret_cast of the 3rd parameter to void pointer. From my understanding you need the 3rd parameter to create tree like structures (or even more sophisticated data structures). For plain tables I've never needed the 3rd createIndex() parameter.

    Best regards
    ars

  6. The following user says thank you to ars for this useful post:

    neuronet (26th December 2014)

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

    Default Re: Source code for createIndex() ?

    ars: your post helps, but I am still confused. I think what I'll do is think about it more, and probably make it into a separate post, as you have already answered my original question about where the source code is, which was the point of this thread

    I'm thinking for the next question it will be more about how to understand what the hell is going on with createIndex, expecially in Python which has no pointers.

    I have actually asked about this, tangentially, here, but never really understood what was going on...
    http://www.qtcentre.org/threads/5974...ally-a-pointer

  8. #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: Source code for createIndex() ?

    Quote Originally Posted by ars View Post
    From my understanding you need the 3rd parameter to create tree like structures (or even more sophisticated data structures). For plain tables I've never needed the 3rd createIndex() parameter.
    The third parameter can be used by a model as an additional reference when row and column are not enough. Which indeed is usually only required for trees.
    But a model is free to use that even for tables or lists, e.g. store the pointer to the row or cell right in the index.

    Cheers,
    _
    Last edited by wysota; 27th December 2014 at 15:38.

  9. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Source code for createIndex() ?

    The third parameter is also defined as a void * pointer, which in reality means it can contain anything that will fit into the size of a pointer (usually the same size as an unsigned long). In Python, you could possibly use this to store the index into a list of objects, for example. The implementation of PySide might even coerce a Python object reference into a pointer "under the hood" so you may be able to store a Python object reference in it.

    But as anda_skoa says, it is rare that you need to use this third parameter unless you are implementing a tree or a proxy model where it would be difficult to navigate the model otherwise.

  10. The following user says thank you to d_stranz for this useful post:

    neuronet (30th December 2014)

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

    Default Re: Source code for createIndex() ?

    Quote Originally Posted by d_stranz View Post
    The third parameter is also defined as a void * pointer, which in reality means it can contain anything that will fit into the size of a pointer (usually the same size as an unsigned long). In Python, you could possibly use this to store the index into a list of objects, for example. The implementation of PySide might even coerce a Python object reference into a pointer "under the hood" so you may be able to store a Python object reference in it.

    But as anda_skoa says, it is rare that you need to use this third parameter unless you are implementing a tree or a proxy model where it would be difficult to navigate the model otherwise.
    Very useful stuff. I am indeed building a tree model by subclassing QAbstractItemModel. I was looking at the implementation of QAbstractTableModel just to check that I understood what I was doing.

    Indeed, in Python, the third input to createIndex is actually just any old Python object, of any type. Typically we just feed in the data item itself to become referred to by the index (e.g., in simpletreemodel example, we give it a TreeItem type directly as the parameter, no pointers or anything like that, which don't exist in Python).

    I have finally started to sort out these issues in a couple of threads over at stack overflow:
    http://stackoverflow.com/questions/2...qt-model-items
    http://stackoverflow.com/questions/2...ject-in-python

    Though I a admit I am still a bit puzzled by the following:
    Qt Code:
    1. inline QModelIndex(const QModelIndex &other)
    2. : r(other.r), c(other.c), p(other.p), m(other.m) {}
    To copy to clipboard, switch view to plain text mode 
    As I said, I am no c++ coder, so this just seems foreign to me. It seems to be creating a QModelIndex out of these r (row), c (column), p (pointer to the data item), and m (I assume the model that it got the index from). But I don't where where it is creating the .row(), .column(), .getInternalPointer(), .model(), and other methods associated with an index. Where is that behavior defined? I can see how it would be fairly easy to get those methods based on the parameters the function is receiving, but would like to see in the source how it all works...
    Last edited by neuronet; 30th December 2014 at 05:08.

  12. #9
    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: Source code for createIndex() ?

    In C++ a method named the same as a class is called a constructor (similar to Python's __init__()). Fields of the class are defined elsewhere - statically in the class declaration as C++ is a strongly typed language which does not allow to add new members to objects during the life of the object. The constructor only initializes those fields with values which is what you can see in the snippet after the colon symbol. The constructor presented is called a copy constructor - it creates an instance from another instance of the same class.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. The following user says thank you to wysota for this useful post:

    neuronet (1st January 2015)

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

    Default Re: Source code for createIndex() ?

    Quote Originally Posted by wysota View Post
    In C++ a method named the same as a class is called a constructor (similar to Python's __init__()). Fields of the class are defined elsewhere - statically in the class declaration as C++ is a strongly typed language which does not allow to add new members to objects during the life of the object. The constructor only initializes those fields with values which is what you can see in the snippet after the colon symbol. The constructor presented is called a copy constructor - it creates an instance from another instance of the same class.
    That is really helpful stuff as I try to make sense of the source. And I missed the class declaration in the header file:
    Direct link to class declaration of QModelIndex

Similar Threads

  1. Source code to *.ui
    By Zergi in forum Qt Tools
    Replies: 3
    Last Post: 28th September 2011, 19:12
  2. no source code
    By banlinhtienphong in forum General Programming
    Replies: 1
    Last Post: 25th July 2011, 18:19
  3. Source code
    By afflictedd2 in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2008, 10:04
  4. source code
    By Colx007 in forum Newbie
    Replies: 5
    Last Post: 19th December 2007, 10:15
  5. Qte source code
    By Gaurav vyas in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 1st July 2007, 15:11

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.