PDA

View Full Version : Tree widget not updating the data



phillip_Qt
17th November 2009, 06:02
Hi All
I need u ppls help. in my treewidget I've added some items. Now i need some child item to a perticular treewidget item. In my code i'm getting the index of item, where i need to insert the child item. but treewidgetitem->addchild(index,treewidgetitem) not updating the child item. Below i've pasted my code. Add addComputers() is the methode where i'm updating child. Can any body tell me what is the wrong in this code. :confused:


Header file
------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H



class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;


private:
//! To collect the list of classrooms and computers added to the coressponding classroom
QList<QTreeWidgetItem *> *m_ClassRoom;
QList<QTreeWidgetItem *> m_computersAddedToCalssroom;
QStringList m_strlistComputer;
QTreeWidgetItem *m_treelist;//(m_strlistComputer);
QList<QTreeWidgetItem *> temp;


private slots:
//! To add Computers
void addComputers();
//! To add classroom
void addClassRoom();

private slots:
//! To handle the right click event in The right side bar
void ContextMenuDisplayMethod(const QPoint &);

private:
QMenu contextMenu;

private:
//! treewidget context menu
QAction * actionClassroom;
QAction * actionComputer;
QList<QAction *> actionsList;

private:
//SingleClass SingleClassobj;
QString m_strIpEntered;
QString m_strClassRoomSel;


//! To add sub Items in tree widget
QModelIndex *m_modelIndex;


//! To add sub Items in tree widget
QTreeWidgetItem *m_Childlist ;
int * index;

};

#endif // MAINWINDOW_H



.cpp File
----
void addComputersToClassroom()
{


m_clientSettingsDlgobj.exec();

m_strIpEntered = m_clientSettingsDlgobj.getIP();;
m_strClassRoomSel = m_clientSettingsDlgobj.getSelClassRoom();

qDebug()<< "MainWindow::addComputers IP Entered : "<<m_strIpEntered;
qDebug()<< "MainWindow::addComputers Cls Room entered : "<<m_strClassRoomSel;

temp = ui->treeWidget->findItems(m_strClassRoomSel , Qt::MatchExactly);

qDebug()<< "MainWindow::addComputers top level size: "<<ui->treeWidget->topLevelItemCount();

QTreeWidgetItemIterator it(ui->treeWidget);
for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
{

if(ui->treeWidget->topLevelItem(i)->text(0) == m_strClassRoomSel)
{

qDebug()<< "MainWindow::addComputers : Inside the iterator: ";

m_Childlist = new QTreeWidgetItem;
m_Childlist->setText(0, m_strIpEntered);
m_Childlist->setText(1, m_strIpEntered);

index = new int;
m_modelIndex = new QModelIndex;
ui->treeWidget->setCurrentItem(temp.at(0));
*m_modelIndex = (ui->treeWidget->currentIndex());
*index = (m_modelIndex->data().toInt());
qDebug()<<"index : "<<*index;// I'm getting the index

m_treelist->insertChild(*index,m_Childlist);// This is not adding the child to classroom

return;
}
}

}

void addClassRoom()
{

bool bOk = false;
QString classroom_name = QInputDialog::getText( this,
tr( "New classroom" ),
tr( "Please enter the name of the classroom you "
"want to create." ),
QLineEdit::Normal, tr( "New classroom" ), &bOk );

if(!classroom_name.isEmpty())
{
bool bTemp = false;
for(int i = 0; i < m_strlistComputer.size(); i++)
{
if(m_strlistComputer.at(i) == classroom_name)
bTemp = true;
}
if(bTemp == false)
{
m_strlistComputer.append(classroom_name);
m_treelist = new QTreeWidgetItem;
m_treelist->setText(0,classroom_name);
m_ClassRoom->append(m_treelist);

ui->treeWidget->addTopLevelItem(m_treelist);
}
}
}

Thank u all.

spirit
17th November 2009, 06:37
why you can't do like this?


for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
{

QTreeWidgetItem *parent = ui->treeWidget->topLevelItem(i);
if(parent->text(0) == m_strClassRoomSel)
{

qDebug()<< "MainWindow::addComputers : Inside the iterator: ";

m_Childlist = new QTreeWidgetItem(parent);
m_Childlist->setText(0, m_strIpEntered);
m_Childlist->setText(1, m_strIpEntered);

return;
}
}


and remove this strange code snippet


...
index = new int;
m_modelIndex = new QModelIndex;
ui->treeWidget->setCurrentItem(temp.at(0));
*m_modelIndex = (ui->treeWidget->currentIndex());
*index = (m_modelIndex->data().toInt());
qDebug()<<"index : "<<*index;// I'm getting the index

m_treelist->insertChild(*index,m_Childlist);// This is not adding the child to classroom
...

phillip_Qt
17th November 2009, 07:00
why you can't do like this?


for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
{

QTreeWidgetItem *parent = ui->treeWidget->topLevelItem(i);
if(parent->text(0) == m_strClassRoomSel)
{

qDebug()<< "MainWindow::addComputers : Inside the iterator: ";

m_Childlist = new QTreeWidgetItem(parent);
m_Childlist->setText(0, m_strIpEntered);
m_Childlist->setText(1, m_strIpEntered);

return;
}
}


and remove this strange code snippet


...
index = new int;
m_modelIndex = new QModelIndex;
ui->treeWidget->setCurrentItem(temp.at(0));
*m_modelIndex = (ui->treeWidget->currentIndex());
*index = (m_modelIndex->data().toInt());
qDebug()<<"index : "<<*index;// I'm getting the index

m_treelist->insertChild(*index,m_Childlist);// This is not adding the child to classroom
...


Thank u. Its working. :)