Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: QTreeWidget->clear()?

  1. #1
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question QTreeWidget->clear()?

    Hi All,

    I m working on Qt 4.1.5 on my MAC.

    I clear a QTreeWidget by calling
    treeWidget->clear()

    Now I want to know that if this function return or emit any signal when the tree is cleared.
    so that I can know when that the reeWidget is cleared.


    If there is somealternative function which can delete all the treeWidgetItem and return me when this process is complete then plz tell me.

    Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget->clear()?

    QTreeWidget::clear() does clear the tree immediately. Do you give it a chance to update itself?
    J-P Nurmi

  3. #3
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Re: QTreeWidget->clear()?

    Thanks for reply.

    Actually what I want is that I have a LinkList of Items which I m showing in QTreeWidget.
    So when I double click a item in the treeWidget I m clearing a tree and showing the child of that QTreeWidgetItem which are in Linked List.


    But as QtreeWidget->clear() takes some time to clear a treeWidget if I emit the signal to show its child then it get crashed.

    So I want that once the treeWidget is cleared then i emit the signal to add new item on the same tree.

    and the application crashes only when i am clearing the treeWidget.

  4. #4
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidget->clear()?

    Aren't you using currentItem(which is NULL after clear()) in method that adds new item?

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget->clear()?

    To me it sounds like you end up having a bunch of invalid pointers in your external linked list structure. Notice that QTreeWidget::clear() deletes items. Use QTreeWidget::takeTopLevelItem(), QTreeWidgetItem::takeChild() or such for taking items out of QTreeWidget without deleting them.
    J-P Nurmi

  6. #6
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTreeWidget->clear()?

    Hi.......

    But If we Use
    Qt Code:
    1. QTreeWidget::takeTopLevelItem(int index)
    To copy to clipboard, switch view to plain text mode 

    then it removes that items only that are inserted at the index we are passing , not all the items that are in the QTreeWidget.

    Thanx
    Always Believe in Urself
    Merry

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget->clear()?

    So what's the problem? Take them all one by one:
    Qt Code:
    1. while (treeWidget->topLevelItemCount() > 0)
    2. treeWidget->takeTopLevelItem(0);
    To copy to clipboard, switch view to plain text mode 

    PS. If I were you, I'd rather switch to model-view approach which is way more flexible in situations like this..
    J-P Nurmi

  8. #8
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTreeWidget->clear()?

    Using this , When I double click on treeWidgetItem then also , program crashes

    Qt Code:
    1. while (treeWidget->topLevelItemCount() > 0)
    2. treeWidget->takeTopLevelItem(0);
    To copy to clipboard, switch view to plain text mode 


    Thanx
    Always Believe in Urself
    Merry

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget->clear()?

    So build the app in debug mode, run it via debugger and see the backtrace to see where exactly it crashes. Otherwise all we can do is to keep guessing for the rest of the month.
    J-P Nurmi

  10. #10
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTreeWidget->clear()?

    Thanx for the reply

    Yaa u are right ,

    Actually , at the time when I double click on the treeWidgetItem to show only its contents and deletes all other , then it Crashes , Otherwise , if i wont clear the treeWidget , then It dont crashes.

    There is a problem , in clearing the tree.

    Thanx
    Always Believe in Urself
    Merry

  11. #11
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget->clear()?

    Hi,
    I think you read the data for the tree from a table ( in a database ). is it?
    if so, first you create a connection with the tree like this

    connect( ManagerTree, SIGNAL( itemDoubleClicked (QTreeWidgetItem*,int) ), this, SLOT( ChkTreeItem(QTreeWidgetItem*, int ) ) );

    and you write a function for create the tree passing the frst level of the item.
    After that, you create another function named as ChkTreeItem(QTreeWidgetItem*, int ) and in that function you read the text item like this

    QString Name = item->text(column);
    Then you get the selected item from the variable Name. Using this Variable you can get the level of the item from the table.
    Then you clear the tree. after you recall the first function with the level.
    I think through this methord you can solve your probs.
    Try it.....

  12. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget->clear()?

    For database data you would use one of available SQL models depending on your needs, and a view:
    Qt Code:
    1. QSqlQueryModel *model = new QSqlQueryModel(parent);
    2. model->setQuery("SELECT * FROM table");
    3. view->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  13. #13
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTreeWidget->clear()?

    Thanx for the reply

    But I am not using database to store items , I am creating linklist for the items , I also made a connection

    Qt Code:
    1. connect(TreeWidgetFolder,SIGNAL(itemDoubleClicked ( QTreeWidgetItem *, int)), this, SLOT(ShowTreeWidgetFolderItem( QTreeWidgetItem *, int) ));
    To copy to clipboard, switch view to plain text mode 

    regards
    Always Believe in Urself
    Merry

  14. #14
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget->clear()?

    Could you paste ShowTreeWidgetFolderItem()? And possibly other relevant code too, for example where you manage the list of items.
    J-P Nurmi

  15. #15
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTreeWidget->clear()?

    Pls preview the code

    Qt Code:
    1. void FileSystem :: ShowTreeWidgetFolderItem( QTreeWidgetItem *item, int col) // Function call -> itemdoubleClicked
    2. {
    3. bool ok;
    4. if(!bStopFlag)
    5. {
    6. QList<QTreeWidgetItem*>m_list;
    7. m_list = FolderTree->selectedItems();
    8. m_id = m_list[0]->text(1).toInt(&ok,10);
    9.  
    10. emit ShowFolders(); //emit Signal to show the contents of the item that is double clicked
    11. }
    12. }
    13.  
    14.  
    15. void FileSystem :: ShowFolderItem() // function to show the contents of the item that is double clicked
    16.  
    17. {
    18. TREEITEM *treeitem;
    19.  
    20. for (int item = 0; item < FolderTree->topLevelItemCount(); ++item)
    21. {
    22. qDeleteAll(FolderTree->topLevelItem(item)->takeChildren());
    23. }
    24. for(unsigned int i = 0;i< list.count();i++)
    25. {
    26. treeitem = list[i];
    27. if(treeitem->MyTreeEntry.dwParentId == m_id)
    28. {
    29. QTreeWidgetItem *RightTreeItem = new QTreeWidgetItem(FolderTree);
    30. RightTreeItem->setText(0,treeitem->bFileName);
    31. RightTreeItem->setCheckState(0,Qt::Unchecked);
    32. RightTreeItem->setText(0,treeitem->bFileName);
    33. RightTreeItem->setText(1,QString::number(treeitem->MyTreeEntry.dwFileId));
    34. RightTreeItem->setText(2,QString::number(treeitem->MyTreeEntry.dwParentId));
    35.  
    36. if(treeitem->MyTreeEntry.bAttribute == 0x10)
    37. RightTreeItem->setText(3,">");
    38.  
    39. RightTreeItem->setCheckState(0,Qt::Unchecked);
    40. FolderTree->resizeColumnToContents (0);
    41. FolderTree->resizeColumnToContents (1);
    42. FolderTree->resizeColumnToContents (2);
    43. }
    44. }
    45. }
    To copy to clipboard, switch view to plain text mode 

    Thanx
    Last edited by merry; 28th September 2007 at 10:01. Reason: updated contents
    Always Believe in Urself
    Merry

  16. #16
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget->clear()?

    Now I'm even more convinced that you really should consider switching to model-view approach. You shouldn't be replicating the whole file system as QTreeWidgetItems but use QDirModel.

    See threads:
    J-P Nurmi

  17. The following user says thank you to jpn for this useful post:

    merry (28th September 2007)

  18. #17
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTreeWidget->clear()?

    Hi Jpn

    I appreciate your suggestion of using "QDirModel" approach but I think this approach is useful for the local fileSystem , It shows all the directories & files of the local filesystem only.

    But If I have to view all the directories & files of some other media , say any Removeable Media , then I think this approach is not useful.


    Regards
    Always Believe in Urself
    Merry

  19. #18
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget->clear()?

    Actually QDirModel is capable of providing information about removable media and even mapped network drives on Windows.

    Alternatively, you could wrap TREEITEM structure into a QAbstractItemModel but it's a bit of work and sure requires some knowledge of Qt's model-view framework.

    However, with QTreeWidgetItems you'll just get lost into jungle. QTreeWidget is not intended to and is not really usable for something like what you're doing.
    J-P Nurmi

  20. #19
    Join Date
    Jun 2006
    Posts
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QTreeWidget->clear()?

    In Qt3 it is not allowed to delete items of the QListView while processing the signals for pressing, clicking or doubleClicking. In that case the Application crashes. Maybe this issue is also existing in the Qt4-Versions.

  21. #20
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTreeWidget->clear()?

    Sorry but I forget to update my profile ,

    Right now , I am working on Qt4.1.5
    Always Believe in Urself
    Merry

Similar Threads

  1. how to add icons to QTreeWidget?
    By wei243 in forum Qt Programming
    Replies: 4
    Last Post: 21st September 2007, 08:34
  2. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 12:20
  3. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32
  4. How to capture resizing of QTreeWidget columns?
    By simk in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2006, 06:10
  5. few questions related to QTreeWidget
    By prakash in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2006, 07:32

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.