Results 1 to 4 of 4

Thread: Associate data with child items in listView

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.