Results 1 to 3 of 3

Thread: Tree widget not updating the data

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

    Default Tree widget not updating the data

    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.

    Qt Code:
    1. Header file
    2. ------
    3. #ifndef MAINWINDOW_H
    4. #define MAINWINDOW_H
    5.  
    6.  
    7.  
    8. class MainWindow : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MainWindow(QWidget *parent = 0);
    14. ~MainWindow();
    15.  
    16. private:
    17. Ui::MainWindow *ui;
    18.  
    19.  
    20. private:
    21. //! To collect the list of classrooms and computers added to the coressponding classroom
    22. QList<QTreeWidgetItem *> *m_ClassRoom;
    23. QList<QTreeWidgetItem *> m_computersAddedToCalssroom;
    24. QStringList m_strlistComputer;
    25. QTreeWidgetItem *m_treelist;//(m_strlistComputer);
    26. QList<QTreeWidgetItem *> temp;
    27.  
    28.  
    29. private slots:
    30. //! To add Computers
    31. void addComputers();
    32. //! To add classroom
    33. void addClassRoom();
    34.  
    35. private slots:
    36. //! To handle the right click event in The right side bar
    37. void ContextMenuDisplayMethod(const QPoint &);
    38.  
    39. private:
    40. QMenu contextMenu;
    41.  
    42. private:
    43. //! treewidget context menu
    44. QAction * actionClassroom;
    45. QAction * actionComputer;
    46. QList<QAction *> actionsList;
    47.  
    48. private:
    49. //SingleClass SingleClassobj;
    50. QString m_strIpEntered;
    51. QString m_strClassRoomSel;
    52.  
    53.  
    54. //! To add sub Items in tree widget
    55. QModelIndex *m_modelIndex;
    56.  
    57.  
    58. //! To add sub Items in tree widget
    59. QTreeWidgetItem *m_Childlist ;
    60. int * index;
    61.  
    62. };
    63.  
    64. #endif // MAINWINDOW_H
    65.  
    66.  
    67.  
    68. .cpp File
    69. ----
    70. void addComputersToClassroom()
    71. {
    72.  
    73.  
    74. m_clientSettingsDlgobj.exec();
    75.  
    76. m_strIpEntered = m_clientSettingsDlgobj.getIP();;
    77. m_strClassRoomSel = m_clientSettingsDlgobj.getSelClassRoom();
    78.  
    79. qDebug()<< "MainWindow::addComputers IP Entered : "<<m_strIpEntered;
    80. qDebug()<< "MainWindow::addComputers Cls Room entered : "<<m_strClassRoomSel;
    81.  
    82. temp = ui->treeWidget->findItems(m_strClassRoomSel , Qt::MatchExactly);
    83.  
    84. qDebug()<< "MainWindow::addComputers top level size: "<<ui->treeWidget->topLevelItemCount();
    85.  
    86. QTreeWidgetItemIterator it(ui->treeWidget);
    87. for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
    88. {
    89.  
    90. if(ui->treeWidget->topLevelItem(i)->text(0) == m_strClassRoomSel)
    91. {
    92.  
    93. qDebug()<< "MainWindow::addComputers : Inside the iterator: ";
    94.  
    95. m_Childlist = new QTreeWidgetItem;
    96. m_Childlist->setText(0, m_strIpEntered);
    97. m_Childlist->setText(1, m_strIpEntered);
    98.  
    99. index = new int;
    100. m_modelIndex = new QModelIndex;
    101. ui->treeWidget->setCurrentItem(temp.at(0));
    102. *m_modelIndex = (ui->treeWidget->currentIndex());
    103. *index = (m_modelIndex->data().toInt());
    104. qDebug()<<"index : "<<*index;// I'm getting the index
    105.  
    106. m_treelist->insertChild(*index,m_Childlist);// This is not adding the child to classroom
    107.  
    108. return;
    109. }
    110. }
    111.  
    112. }
    113.  
    114. void addClassRoom()
    115. {
    116.  
    117. bool bOk = false;
    118. QString classroom_name = QInputDialog::getText( this,
    119. tr( "New classroom" ),
    120. tr( "Please enter the name of the classroom you "
    121. "want to create." ),
    122. QLineEdit::Normal, tr( "New classroom" ), &bOk );
    123.  
    124. if(!classroom_name.isEmpty())
    125. {
    126. bool bTemp = false;
    127. for(int i = 0; i < m_strlistComputer.size(); i++)
    128. {
    129. if(m_strlistComputer.at(i) == classroom_name)
    130. bTemp = true;
    131. }
    132. if(bTemp == false)
    133. {
    134. m_strlistComputer.append(classroom_name);
    135. m_treelist = new QTreeWidgetItem;
    136. m_treelist->setText(0,classroom_name);
    137. m_ClassRoom->append(m_treelist);
    138.  
    139. ui->treeWidget->addTopLevelItem(m_treelist);
    140. }
    141. }
    142. }
    To copy to clipboard, switch view to plain text mode 

    Thank u all.
    Cheers,
    Phillip



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

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Tree widget not updating the data

    why you can't do like this?
    Qt Code:
    1. for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
    2. {
    3.  
    4. QTreeWidgetItem *parent = ui->treeWidget->topLevelItem(i);
    5. if(parent->text(0) == m_strClassRoomSel)
    6. {
    7.  
    8. qDebug()<< "MainWindow::addComputers : Inside the iterator: ";
    9.  
    10. m_Childlist = new QTreeWidgetItem(parent);
    11. m_Childlist->setText(0, m_strIpEntered);
    12. m_Childlist->setText(1, m_strIpEntered);
    13.  
    14. return;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    and remove this strange code snippet
    Qt Code:
    1. ...
    2. index = new int;
    3. m_modelIndex = new QModelIndex;
    4. ui->treeWidget->setCurrentItem(temp.at(0));
    5. *m_modelIndex = (ui->treeWidget->currentIndex());
    6. *index = (m_modelIndex->data().toInt());
    7. qDebug()<<"index : "<<*index;// I'm getting the index
    8.  
    9. m_treelist->insertChild(*index,m_Childlist);// This is not adding the child to classroom
    10. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    phillip_Qt (17th November 2009)

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

    Default Re: Tree widget not updating the data

    Quote Originally Posted by spirit View Post
    why you can't do like this?
    Qt Code:
    1. for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
    2. {
    3.  
    4. QTreeWidgetItem *parent = ui->treeWidget->topLevelItem(i);
    5. if(parent->text(0) == m_strClassRoomSel)
    6. {
    7.  
    8. qDebug()<< "MainWindow::addComputers : Inside the iterator: ";
    9.  
    10. m_Childlist = new QTreeWidgetItem(parent);
    11. m_Childlist->setText(0, m_strIpEntered);
    12. m_Childlist->setText(1, m_strIpEntered);
    13.  
    14. return;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    and remove this strange code snippet
    Qt Code:
    1. ...
    2. index = new int;
    3. m_modelIndex = new QModelIndex;
    4. ui->treeWidget->setCurrentItem(temp.at(0));
    5. *m_modelIndex = (ui->treeWidget->currentIndex());
    6. *index = (m_modelIndex->data().toInt());
    7. qDebug()<<"index : "<<*index;// I'm getting the index
    8.  
    9. m_treelist->insertChild(*index,m_Childlist);// This is not adding the child to classroom
    10. ...
    To copy to clipboard, switch view to plain text mode 
    Thank u. Its working.
    Cheers,
    Phillip



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

Similar Threads

  1. Qtreewidget not updating data
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2009, 05:10
  2. Replies: 0
    Last Post: 15th May 2009, 16:12
  3. Qt Tree Widget Question
    By alavtec in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2008, 17:21
  4. Tree widget sorting
    By ganeshshenoy in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2008, 14:06
  5. Drag from tree widget
    By EricF in forum Qt Programming
    Replies: 9
    Last Post: 18th December 2007, 00:09

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.