Results 1 to 12 of 12

Thread: How to add subitem in treewidget

  1. #1
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to add subitem in treewidget

    Hi All,
    i ve a treewidget. if i click right button of mouse, context menu is appearing, where there are two option, 1 is to add parent. and another 2 add childi. suppose i ve added 3 parent, parent1, parent2, parent3. if i need to add a child a new dialog ll be appear. where i can give the child name in a lienedit and one combo box to select the parent name. in my tree i've to update child1 naem as sub item under that parent.'
    e.g i gave child1 to add under parent1
    |--parent1
    | |
    | |-child1
    | |-child2
    |
    |--parent2
    | |
    | |-child21
    | |-child22
    |
    |--parent3
    |
    |
    |
    My problem is that how to insert the sub item, child under the perticular parent in the tree widget.
    Below is my code.

    QTreeWidgetItem *m_treelist;
    QString parent, child;// these 2 are my inputs.
    QList<QTreeWidgetItem *> temp;
    temp = ui->treeWidget->findItems(parent , Qt::MatchExactly);
    After that how ll i insert the child in that parent??? plz help me.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to add subitem in treewidget

    QTreeWidgetItem::addChild()? Or what you are seeking for?

  3. #3
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add subitem in treewidget

    Quote Originally Posted by Lykurg View Post
    QTreeWidgetItem::addChild()? Or what you are seeking for?
    I need to add the child under the particular parent.
    for e.g Qstring paretn, child; my i/p
    how will i search the parent(i/p sQString parent) in treewidget item and add this child under that parent.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  4. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to add subitem in treewidget

    Quote Originally Posted by phillip_Qt View Post
    I need to add the child under the particular parent.
    for e.g Qstring paretn, child; my i/p
    how will i search the parent(i/p sQString parent) in treewidget item and add this child under that parent.
    Isn't it clear with function name itself "addChild" ?
    Call addChild(), from the reference of the parent item. And new item will be added as child.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to add subitem in treewidget

    Ok,

    first you could determinate where the context menu was requested and search the corresponding top level item. But if you have the string of that parent item (and it is unique..) you can use:

    Qt Code:
    1. QTreeWidgetItem *m_treelist;
    2. QString parent, child;// these 2 are my inputs.
    3. QList<QTreeWidgetItem *> temp;
    4. temp = ui->treeWidget->findItems(parent , Qt::MatchExactly);
    5. if (!temp.isEmpty())
    6. {
    7. it->setText(0, child);
    8. temp.at(0)->addChild(it);
    9. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add subitem in treewidget

    Quote Originally Posted by yogeshgokul View Post
    Isn't it clear with function name itself "addChild" ?
    Call addChild(), from the reference of the parent item. And new item will be added as child.
    I think i didn't able to describe my problem properly. In my application, if i dbl click on tree widget a context menu ll pop up, having 2 option, either to add classroom or computers. after adding classroom i need to add some computers to a perticular classroom. I'm not able how to add the child. I've added my code. in my mainwindow i've amethod like addComputers()
    here i m not able how to add the computers under the perticular class i've choosen.
    Attached Files Attached Files
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to add subitem in treewidget

    Hi,

    first alter clientSettingsDialog::getSelClassRoom() that it returns a pointer to the tree item! Therefore you also have to alter clientSettingsDialog::setClassComboBox().

    Second (even if your logic is more then strange...):

    Qt Code:
    1. if(temp.at(i) == m_treelist)
    2. {
    3. qDebug()<< "MainWindow::addComputers : Inside the loop: ";
    4. it->setText(0, m_strIpEntered);
    5. temp.at(i)->addChild(it);
    6. // m_treelist->insertChild(i, temp.at(i));
    7. // ui->treeWidget->addTopLevelItem(m_treelist);
    8. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add subitem in treewidget

    Quote Originally Posted by Lykurg View Post
    Hi,

    first alter clientSettingsDialog::getSelClassRoom() that it returns a pointer to the tree item! Therefore you also have to alter clientSettingsDialog::setClassComboBox().

    Second (even if your logic is more then strange...):

    Qt Code:
    1. if(temp.at(i) == m_treelist)
    2. {
    3. qDebug()<< "MainWindow::addComputers : Inside the loop: ";
    4. it->setText(0, m_strIpEntered);
    5. temp.at(i)->addChild(it);
    6. // m_treelist->insertChild(i, temp.at(i));
    7. // ui->treeWidget->addTopLevelItem(m_treelist);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Hi Lykurg
    Thanks for the reply. Actually i'm setting the combobox value in another file. So how ll i be able to return a pointer to the treewidget?
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to add subitem in treewidget

    Quote Originally Posted by phillip_Qt View Post
    Hi Lykurg
    Thanks for the reply. Actually i'm setting the combobox value in another file. So how ll i be able to return a pointer to the treewidget?
    Instead of only giving a string to setClassComboBox(), you also can give an additional pointer to the item. Then store it internally and when you call getSelClassRoom() give the pointer back. E.g.
    Qt Code:
    1. QHash<int, QTreeWidgetItem*> internalHash;
    2. // int = position of your combo box
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add subitem in treewidget

    Quote Originally Posted by Lykurg View Post
    Instead of only giving a string to setClassComboBox(), you also can give an additional pointer to the item. Then store it internally and when you call getSelClassRoom() give the pointer back. E.g.
    Qt Code:
    1. QHash<int, QTreeWidgetItem*> internalHash;
    2. // int = position of your combo box
    To copy to clipboard, switch view to plain text mode 
    Is there any otherway to find the index of parent by using string?
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  11. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to add subitem in treewidget

    Quote Originally Posted by phillip_Qt View Post
    Is there any otherway to find the index of parent by using string?
    No only as you do with findItems, but that's not very elegant and can easily lead to errors. So working with pointers is the better way. And your case is a standard case for doing so.

    What is exactly your problem with the pointers?

  12. #12
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add subitem in treewidget

    Quote Originally Posted by Lykurg View Post
    No only as you do with findItems, but that's not very elegant and can easily lead to errors. So working with pointers is the better way. And your case is a standard case for doing so.

    What is exactly your problem with the pointers?
    Thanks Lykurg.
    I dont ve any problem with pointers, but cannt understand how to implement the logic you told, in my code.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

Similar Threads

  1. treewidget and a message system
    By alisami in forum Qt Programming
    Replies: 7
    Last Post: 27th July 2008, 18:05
  2. How to set the sizehint for treeWidget
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2007, 11:35
  3. By default TreeWidget width setting in DockWidget
    By santosh.kumar in forum Qt Programming
    Replies: 0
    Last Post: 31st May 2007, 05:55
  4. Replies: 2
    Last Post: 29th May 2007, 13:55
  5. How i will use treeWidget of GUI class in thread class
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2007, 15:31

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.