PDA

View Full Version : How to add subitem in treewidget



phillip_Qt
16th November 2009, 09:16
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.:(

Lykurg
16th November 2009, 09:33
QTreeWidgetItem::addChild()? Or what you are seeking for?

phillip_Qt
16th November 2009, 09:37
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.
:(

yogeshgokul
16th November 2009, 09:46
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. ;)

Lykurg
16th November 2009, 09:50
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:


QTreeWidgetItem *m_treelist;
QString parent, child;// these 2 are my inputs.
QList<QTreeWidgetItem *> temp;
temp = ui->treeWidget->findItems(parent , Qt::MatchExactly);
if (!temp.isEmpty())
{
QTreeWidgetItem *it = new QTreeWidgetItem();
it->setText(0, child);
temp.at(0)->addChild(it);
}

phillip_Qt
16th November 2009, 09:57
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. :(

Lykurg
16th November 2009, 10:15
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...):


if(temp.at(i) == m_treelist)
{
qDebug()<< "MainWindow::addComputers : Inside the loop: ";
QTreeWidgetItem *it = new QTreeWidgetItem();
it->setText(0, m_strIpEntered);
temp.at(i)->addChild(it);
// m_treelist->insertChild(i, temp.at(i));
// ui->treeWidget->addTopLevelItem(m_treelist);
}

phillip_Qt
16th November 2009, 10:37
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...):


if(temp.at(i) == m_treelist)
{
qDebug()<< "MainWindow::addComputers : Inside the loop: ";
QTreeWidgetItem *it = new QTreeWidgetItem();
it->setText(0, m_strIpEntered);
temp.at(i)->addChild(it);
// m_treelist->insertChild(i, temp.at(i));
// ui->treeWidget->addTopLevelItem(m_treelist);
}
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?

Lykurg
16th November 2009, 10:40
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.
QHash<int, QTreeWidgetItem*> internalHash;
// int = position of your combo box

phillip_Qt
16th November 2009, 11:10
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.
QHash<int, QTreeWidgetItem*> internalHash;
// int = position of your combo box

Is there any otherway to find the index of parent by using string?

Lykurg
16th November 2009, 11:15
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?

phillip_Qt
16th November 2009, 11:22
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.