Results 1 to 4 of 4

Thread: add child item in list view

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2012
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default add child item in list view

    Hi,
    i create list view , i set model to the list like that
    Qt Code:
    1. listView->setModel( model );
    To copy to clipboard, switch view to plain text mode 
    i createthe model like that
    Qt Code:
    1. void MainWindow::createModel()
    2. {
    3. model = new QStandardItemModel( 0,0,this );
    4. model->setHorizontalHeaderLabels( QStringList() << "liste" );
    5. createModelItems();
    6. }
    7.  
    8. void MainWindow::createModelItems()
    9. {
    10. QStringList choiceList;
    11. QStandardItem *pItem;
    12.  
    13. choiceList << "conf1" << "conf2" << "conf3";
    14. foreach( QString str, choiceList ){ // parent
    15. pItem = new QStandardItem( str );
    16. pItem->setTristate( true );
    17. model->appendRow( pItem );
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    and i added new item like that with signal slot
    the sig,al is
    Qt Code:
    1. void MainWindow::add(vector3 point)
    2. {
    3. QStandardItem *item=new QStandardItem(QString ("Marker %0").arg(nb_mark));
    4. nb_mark++;
    5. emit itemAded(item);
    6. }
    To copy to clipboard, switch view to plain text mode 
    the slot is
    Qt Code:
    1. void MainWindow::itemAded( QStandardItem *pItem )
    2. {
    3.  
    4. model->appendRow( pItem );
    5. }
    To copy to clipboard, switch view to plain text mode 
    but the problem is that it add to me new item and that's not what i want
    i want to add the new item as child of an item of the "chois list"

    i know that i must add this infiormation in the signal , but how i do that ? have you an idea ?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: add child item in list view

    and i added new item like that with signal slot
    the sig,al is
    Do not implement/define signal method. (Qt will define it). Move the code in the signal into slot and remove the signal definition.

    i want to add the new item as child of an item of the "chois list"
    List items cannot have child item, you should use Tree (TreeView) if you want to add new items as children.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Dec 2012
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: add child item in list view

    hi,
    Do not implement/define signal method. (Qt will define it). Move the code in the signal into slot and remove the signal definition.
    no i must add it , because it's important and there is another things i must made it in the signal.
    List items cannot have child item, you should use Tree (TreeView) if you want to add new items as children.
    if i use treeview i will change à lot the code ?
    another thing i see in some exemple thy used listview with child
    Qt Code:
    1. void MainWindow::createModelItems()
    2. {
    3. QStringList choiceList, optionList;
    4.  
    5. choiceList << "Alpha" << "Beta" << "Charlie";
    6. optionList << "Opt1" << "Opt2" << "Opt3";
    7.  
    8. QStandardItem *pItem, *cItem;
    9.  
    10. foreach( QString str, choiceList ){ // parent
    11. pItem = new QStandardItem( str );
    12. pItem->setCheckable( true );
    13. pItem->setTristate( true );
    14. foreach( QString str, optionList ){ // children
    15. cItem = new QStandardItem( str );
    16. cItem->setCheckable( true );
    17. pItem->appendRow( cItem );
    18. }
    19.  
    20. model->appendRow( pItem );
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: add child item in list view

    hi,

    Do not implement/define signal method. (Qt will define it). Move the code in the signal into slot and remove the signal definition.
    no i must add it , because it's important and there is another things i must made it in the signal.
    The signal still exists, I mean only the signal declaration should be there (class.h), and signal definition/implementation (class.cpp) should not exists. Qt framework creates the implementation for you.


    List items cannot have child item, you should use Tree (TreeView) if you want to add new items as children.
    if i use treeview i will change à lot the code ?
    another thing i see in some exemple thy used listview with child
    The code snipet is using QStandardItemModel, this model has to set on a QTreeView.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 1
    Last Post: 14th November 2012, 21:00
  2. Replies: 3
    Last Post: 12th February 2012, 23:00
  3. QT Model View - parent Item different from child
    By mromanuk in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2011, 18:21
  4. list view displaying same item value...
    By addu in forum Newbie
    Replies: 1
    Last Post: 30th May 2009, 01:55
  5. list view item problem..
    By addu in forum Qt Programming
    Replies: 5
    Last Post: 29th May 2009, 11:26

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.