Results 1 to 4 of 4

Thread: How to access the class object on click of QTreeView Item

  1. #1
    Join Date
    Jun 2018
    Location
    India
    Posts
    11
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question How to access the class object on click of QTreeView Item

    Hi,

    Consider the following two structures, where "Contact" is the main structure which has list of "Address" objects as one among its data member.

    struct Address
    {
    QString pincode;
    QList<QString> telephone;
    };

    struct Contact
    {
    QString firstName;
    QString secondName;
    QString address;
    QList<Address*> addressList;
    };

    I created a QTreeView which inherits from QAbstractItemModel. Each QTreeViewItem parent should be mapped to "Contact" data members where the "addressList" should be mapped as a child item for that particular parent. I am trying to write a user defined function logic to parse the structure members and add as a Parent/child accordingly.

    My use case is that , when i click the child row (addressList), i need to retreive the entire Address object but not the Contact object. How can i achieve this using ModelIndex?

    TIA

  2. #2
    Join Date
    Jun 2018
    Location
    India
    Posts
    11
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to access the class object on click of QTreeView Item

    Hi, Can anyone help me on this

  3. #3
    Join Date
    Jul 2008
    Posts
    377
    Thanks
    9
    Thanked 47 Times in 45 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to access the class object on click of QTreeView Item

    Hi, you can store any data you like (e.g. pointers to your structs) using QAbstractItemModel::setData() using Qt::UserRole or higher values for the "role" parameter.

    Ginsengelf

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,812
    Thanks
    216
    Thanked 590 Times in 580 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to access the class object on click of QTreeView Item

    And you can return this pointer to the outside world when you implement QAbstractItemModel::createIndex() for your tree model. The third, optional argument to this method is a void * pointer. Your program can retrieve that pointer from the QModelIndex instance using the QModelIndex::internalPointer() method.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 3
    Last Post: 8th December 2015, 23:20
  2. Replies: 2
    Last Post: 19th May 2014, 19:25
  3. Access caller qml-Object from C++ Object
    By xdn in forum Qt Quick
    Replies: 5
    Last Post: 14th February 2013, 10:58
  4. Replies: 4
    Last Post: 26th June 2007, 20:19
  5. Replies: 3
    Last Post: 16th May 2007, 12:07

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.