+ Reply to Thread
Results 1 to 4 of 4

Thread: QTreeView restore Expanded node after reload model

  1. #1
    Join Date
    May 2006
    Posts
    774
    Thanks
    49
    Thanked 46 Times in 44 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTreeView restore Expanded node after reload model

    I insert on a QMap each first level node if is Expanded or not
    CollState.insert(cid,isExpanded(node) ? 1 : 0 );

    after user chance the page order criteria from a node i must reload the model and restore
    all expanded item state..

    this is run only on first level ..
    How i can register the state from child ?

    node.child show not valid ... but exist!

    i can not use expandAll the are to many childs..

    Qt Code:
    1. void PageList::savestate()
    2. {
    3. /* enum COLLAPSENR { COLLAPSEID = Qt::UserRole + 20 }; */
    4. CollState.clear(); /* QMap<int,int> CollState; clear old state */
    5. for (int r = 0; r < model->rowCount(); ++r) {
    6. for (int c = 0; c < model->columnCount(); ++c) {
    7. QModelIndex node = model->index(r,c);
    8. savestate(node);
    9. }
    10. }
    11. }
    12.  
    13. void PageList::savestate( const QModelIndex node )
    14. {
    15. if (!node.isValid()) {
    16. return;
    17. }
    18. const int cid = node.data(COLLAPSEID).toInt();
    19. if (CollState[cid]) { /* is register on qmap ? after clear */
    20. return;
    21. }
    22. CollState.insert(cid,isExpanded(node) ? 1 : 0 );
    23. savestate(node.child(node.row(),node.column()));
    24. }
    25.  
    26.  
    27. /* restore if expanded or not */
    28.  
    29. void PageList::staterestore( const QModelIndex node )
    30. {
    31. if (!node.isValid()) {
    32. return;
    33. }
    34. const int cid = node.data(COLLAPSEID).toInt();
    35. if (!CollState[cid]) {
    36. return;
    37. }
    38. if (CollState[cid] == 1) {
    39. setExpanded(node,true);
    40. }
    41. staterestore(node.child(node.row(),node.column()));
    42. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QTreeView restore Expanded node after reload model

    Is it possible that the hierarchy changes when you reload the model or does it remain the same?
    J-P Nurmi

  3. #3
    Join Date
    May 2006
    Posts
    774
    Thanks
    49
    Thanked 46 Times in 44 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeView restore Expanded node after reload model

    Quote Originally Posted by jpn View Post
    Is it possible that the hierarchy changes when you reload the model or does it remain the same?
    no remain the same ....

    I solved on this way ... colums 0 from Category having a nummer and 1 the name
    and level 3 pages not...
    Now i must only write QMap to Qsetting .

    Qt Code:
    1. void PageList::savestate()
    2. {
    3. /* enum COLLAPSENR { COLLAPSEID = Qt::UserRole + 20 }; */
    4. CollState.clear(); /* QMap<int,int> CollState; */
    5. for (int r = 0; r < model->rowCount(); ++r) {
    6. for (int c = 0; c < model->columnCount(); ++c) {
    7. QModelIndex node = model->index(r,c);
    8. savestate(node);
    9. }
    10. }
    11. qDebug() << "### SAVE SIZEeeeeeeeeee " << CollState.size();
    12. }
    13.  
    14. void PageList::savestate( const QModelIndex node , bool fromchild )
    15. {
    16. if (!node.isValid()) {
    17. return;
    18. }
    19. if (node.data().toString().toInt() < 1 || node.column() !=0 ) {
    20. return;
    21. }
    22. const int cid = node.data().toString().toInt();
    23. if (CollState[cid]) { /* is register on qmap ? after clear */
    24. return;
    25. }
    26. CollState.insert(cid,isExpanded(node) ? 1 : 0 );
    27. QStandardItem *item = model->itemFromIndex(node);
    28. if ( item->hasChildren() ) {
    29. for (int i = 0; i < limit_page; ++i) {
    30. savestate(node.child(i,0),true);
    31. }
    32. }
    33. }
    34.  
    35.  
    36. void PageList::RestoreCollapseState()
    37. {
    38. if (CollState.size() < 1) {
    39. return;
    40. }
    41. for (int r = 0; r < model->rowCount(); ++r) {
    42. for (int c = 0; c < model->columnCount(); ++c) {
    43. QModelIndex node = model->index(r,c);
    44. staterestore(node);
    45. }
    46. }
    47. }
    48.  
    49. void PageList::staterestore( const QModelIndex node )
    50. {
    51. if (!node.isValid()) {
    52. return;
    53. }
    54. if (node.data().toString().toInt() < 1 || node.column() !=0 ) {
    55. return;
    56. }
    57. const int cid = node.data().toString().toInt();
    58. if (!CollState[cid]) {
    59. return;
    60. }
    61. if (CollState[cid] == 1) {
    62. ////////qDebug() << "### expand on name " << node.data().toString();
    63. ///////////qDebug() << "### expand on " << node.row() << node.column();
    64. setExpanded(node,true);
    65. }
    66. QStandardItem *item = model->itemFromIndex(node);
    67. if ( item->hasChildren() ) {
    68. for (int i = 0; i < limit_page; ++i) {
    69. staterestore(node.child(i,0));
    70. }
    71. }
    72. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QTreeView restore Expanded node after reload model

    I did it once like this for a static QTreeWidget:
    Qt Code:
    1. void saveTreeState()
    2. {
    3. QVariantList states;
    4. QTreeWidgetItemIterator it(treeWidget);
    5. while (*it)
    6. {
    7. states += (*it)->isExpanded();
    8. ++it;
    9. }
    10. QSettings settings;
    11. settings.setValue("states", states);
    12. }
    13.  
    14. void restoreTreeState()
    15. {
    16. QSettings settings;
    17. QVariantList states = settings.value("states").toList();
    18. QTreeWidgetItemIterator it(treeWidget);
    19. while (*it && !states.isEmpty())
    20. {
    21. (*it)->setExpanded(states.takeFirst().toBool());
    22. ++it;
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 
    Of course, it doesn't apply in your situation Patrik because you're using a model, not QTreeWidget. But perhaps it turns out to be useful for someone who later searches for a solution on the same subject.
    J-P Nurmi

+ Reply to Thread

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