Results 1 to 4 of 4

Thread: Associate data with child items in listView

  1. #1
    Join Date
    Dec 2010
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Associate data with child items in listView

    Hello folks,
    I have spent hours of time on how to assign data to a child item in a listView.
    What's wrong? Please help!

    Qt Code:
    1. #include <QApplication>
    2. #include <QtDebug>
    3. #include <QtCore>
    4. #include <QtGui>
    5.  
    6. int main(int argc, char** argv)
    7. {
    8. QApplication app(argc, argv);
    9. QMainWindow window;
    10.  
    11. QListView *listView1=new QListView;
    12. QListView *listView2=new QListView;
    13. sp->addWidget(listView1);
    14. sp->addWidget(listView2);
    15.  
    16.  
    17. QStandardItem *group1 = new QStandardItem("Music Group 1");
    18.  
    19. QStandardItem *music1=new QStandardItem("Music 1");
    20. music1->setData("Some datails", Qt::UserRole);
    21. qDebug()<<model.data(model.index(0,0).child(0,0), Qt::UserRole);
    22.  
    23. group1->appendRow(music1);
    24.  
    25. model.appendRow(group1);
    26.  
    27. QStandardItem *group2 = new QStandardItem("Music Group 2");
    28.  
    29. QStandardItem *music2=new QStandardItem("Music 2");
    30. qDebug()<<model.data(model.index(1,0).child(0,0), Qt::UserRole);
    31.  
    32. group2->appendRow(music2);
    33.  
    34. model.appendRow(group2);
    35.  
    36. listView1->setModel(&model);
    37. listView1->setRootIndex(QModelIndex());
    38. listView2->setModel(&model);
    39.  
    40. QObject::connect(listView1, SIGNAL(clicked(const QModelIndex&)),
    41. listView2, SLOT(setRootIndex(const QModelIndex&)));
    42.  
    43. window.setCentralWidget(sp);
    44. window.show();
    45.  
    46. return app.exec();
    47. }
    To copy to clipboard, switch view to plain text mode 

    Result:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 

    It seems that the parent items are showed fine, and the corresponding data are retrieved. But the child item & its data are invalid.

    Any advice is grateful!
    Last edited by moiit; 11th August 2011 at 11:30.

  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: Associate data with child items in listView

    You didn't create any child items. You have a flat model with two rows and one column.
    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.


  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Associate data with child items in listView

    See examples how to build tree data model here.
    And:
    Qt Code:
    1. model.index(0, 0).child(0,0);
    To copy to clipboard, switch view to plain text mode 
    QModelIndex::child

  4. #4
    Join Date
    Dec 2010
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Associate data with child items in listView

    Quote Originally Posted by wysota View Post
    You didn't create any child items. You have a flat model with two rows and one column.
    Quote Originally Posted by MarekR22 View Post
    See examples how to build tree data model here.
    And:
    Qt Code:
    1. model.index(0, 0).child(0,0);
    To copy to clipboard, switch view to plain text mode 
    QModelIndex::child
    Thanks for advice!
    I just edited the codes, please see in thread.
    The child items can be showed in listView2, but still can not retrieve data from them.


    Added after 22 minutes:


    OK! Got it!
    Qt Code:
    1. model.setData(model.index(1,0).child(0,0), "Some details", Qt::UserRole);
    2. qDebug()<<model.data(model.index(1,0).child(0,0), Qt::UserRole);
    To copy to clipboard, switch view to plain text mode 
    this way, it works as expected.

    A lot of thanks for you guys!
    Last edited by moiit; 11th August 2011 at 12:00.

Similar Threads

  1. Getting Child Items from QStandardItemModel
    By nikhilqt in forum Qt Programming
    Replies: 4
    Last Post: 4th June 2013, 15:10
  2. Replies: 0
    Last Post: 18th February 2011, 08:24
  3. Replies: 0
    Last Post: 21st October 2010, 06:14
  4. Associate userData to items added to a QListWidget.
    By schall_l in forum Qt Programming
    Replies: 3
    Last Post: 4th August 2009, 12:24
  5. Custom Listview with all-time editable items
    By ivan.cukic in forum Qt Programming
    Replies: 5
    Last Post: 6th September 2006, 15:15

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.