Results 1 to 17 of 17

Thread: How to stop calling data() function for the child items in a tree view initially

  1. #1
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to stop calling data() function for the child items in a tree view initially

    Hi,
    I am implementing a tree view where initially I am showing all the parents, all the child items are collapsed.
    but when initially I show my tree view, data() function is calling for all the items which are collapsed(childs) also.
    How to prevent the data function calling for collapsed child items also, I want my data function to be called only for the items which are visible.

    I have customized QAbstractItemModel, QTreeView and using them.

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop calling data() function for the child items in a tree view initially

    Quote Originally Posted by DURGAPRASAD NEELAM View Post
    I have customized QAbstractItemModel, QTreeView and using them.
    How are we supposed to help if we don't know what you had customized?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop calling data() function for the child items in a tree view initially

    I have re-implemented below functions in my model.


    Qt Code:
    1. QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const ;
    2. bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
    3. Qt::ItemFlags flags( const QModelIndex &index ) const;
    4. QVariant headerData( int section, Qt::Orientation orientatio, int role = Qt::DisplayRole ) const;
    5. virtual QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
    6. virtual QModelIndex parent( const QModelIndex &index ) const;
    7. int rowCount( const QModelIndex &parent = QModelIndex() ) const;
    8. virtual bool hasChildren ( const QModelIndex & parent = QModelIndex() ) const;
    9. int columnCount( const QModelIndex &parent = QModelIndex() ) const { return m_columnCount; }
    To copy to clipboard, switch view to plain text mode 

    And I am using proxy model too for filtering data, here i re-implement filterAcceptsRow().
    If more info is needed, Please let me know.

    Thnaks.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop calling data() function for the child items in a tree view initially

    Yes, more info is needed
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop calling data() function for the child items in a tree view initially

    here is the implementation Part of Model.
    if more info is needed, Please let me know exactly what is needed :-)

    Note : Entire implementation (model, Item) is from Sample tree model example in Qt only (It fits for my requirement so, I just edited that example)

    //proxy
    Qt Code:
    1. ProxyModel::ProxyModel( QObject *parent ): QSortFilterProxyModel( parent )
    2. {
    3. setDynamicSortFilter( false );
    4. }
    5. ProxyModel::~ProxyModel()
    6. {
    7. }
    8. bool ProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
    9. {
    10. return filterLogic();
    11. }
    To copy to clipboard, switch view to plain text mode 


    //Model
    Qt Code:
    1. Model::Model(const QList<QList<QVariant> > &lines,const QList<int>& positions)
    2. {
    3. QList<QVariant> rootData;
    4. m_rootItem = new Item( rootData );
    5. setupModelData( lines, positions, m_rootItem );
    6. }
    7.  
    8. Model::~Model()
    9. {
    10. delete m_rootItem;
    11. }
    12.  
    13. Item* Model::getRootItem() const
    14. {
    15. return m_rootItem;
    16. }
    17.  
    18. QVariant Model::data( const QModelIndex &index, int role ) const
    19. {
    20. if ( ! index.isValid() )
    21. return QVariant();
    22.  
    23. Item *item = static_cast<Item*>( index.internalPointer() );
    24. switch( role )
    25. {
    26. case Qt::DisplayRole:
    27. {
    28. return item->data( index.column(),Qt::DisplayRole );
    29. break;
    30. }
    31. }
    32. return QVariant();
    33. }
    34.  
    35. bool Model::setData( const QModelIndex &index, const QVariant &value, int role )
    36. {
    37. if (( role != Qt::EditRole && role != Qt::DecorationRole && role != Qt::ForegroundRole && role != Qt::BackgroundRole ) || !index.isValid() )
    38. return false;
    39.  
    40. /* Get the item of this index */
    41. Item *item = static_cast<Item*>( index.internalPointer() );
    42.  
    43. /* Editing this item */
    44. bool result = item->setData(index, value, role);
    45.  
    46. if (result)
    47. emit dataChanged( index, index );
    48.  
    49. return result;
    50. }
    51.  
    52. Qt::ItemFlags Model::flags( const QModelIndex &index ) const
    53. {
    54. if ( ! index.isValid() )
    55. return 0;
    56. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    57. }
    58.  
    59. QVariant Model::headerData( int section, Qt::Orientation orientation, int role ) const
    60. {
    61. if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
    62. {
    63. return m_rootItem->data( section,Qt::DisplayRole );
    64. }
    65. return QVariant();
    66. }
    67.  
    68. QModelIndex Model::index( int row, int column, const QModelIndex &parent ) const
    69. {
    70. if ( ! hasIndex(row, column, parent) )
    71. return QModelIndex();
    72.  
    73. Item *parentItem;
    74. if ( ! parent.isValid() )
    75. parentItem = m_rootItem;
    76. else
    77. parentItem = static_cast<Item*>( parent.internalPointer() );
    78.  
    79. Item *childItem = parentItem->child( row );
    80. if ( childItem )
    81. return createIndex( row, column, childItem );
    82. else
    83. return QModelIndex();
    84. }
    85.  
    86. QModelIndex Model::parent( const QModelIndex &index ) const
    87. {
    88. if ( ! index.isValid() )
    89. return QModelIndex();
    90.  
    91. Item *childItem = static_cast<Item*>( index.internalPointer() );
    92. Item *parentItem = childItem->parent();
    93.  
    94. if ( parentItem == m_rootItem )
    95. return QModelIndex();
    96.  
    97. if( parentItem == NULL )
    98. {
    99. return QModelIndex();
    100. }
    101. return createIndex( parentItem->row(), 0, parentItem );
    102. }
    103.  
    104. int Model::rowCount( const QModelIndex &parent ) const
    105. {
    106. Item *parentItem;
    107. if ( parent.column() > 0 )
    108. return 0;
    109.  
    110. if ( ! parent.isValid() )
    111. parentItem = m_rootItem;
    112. else
    113. parentItem = static_cast<Item*>( parent.internalPointer() );
    114.  
    115. return parentItem->childCount();
    116. }
    117.  
    118. bool Model::hasChildren ( const QModelIndex & parent ) const
    119. {
    120. Item *parentItem;
    121. if ( parent.column() > 0 )
    122. return false;
    123.  
    124. if ( ! parent.isValid() )
    125. parentItem = m_rootItem;
    126. else
    127. parentItem = static_cast<Item*>( parent.internalPointer() );
    128.  
    129. return parentItem->hasChildren();
    130. }
    131.  
    132. void Model::setupModelData(const QList<QList<QVariant> >& lines, const QList<int>& positions, Item *parent)
    133. {
    134. //same logic from Sample tree example in Qt
    135. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by DURGAPRASAD NEELAM; 9th May 2015 at 19:13.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop calling data() function for the child items in a tree view initially

    If you want to nail down the problem then get rid of all elements that do not change the test result. If it still doesn't work without a proxy model then remove it from the equation and repeat the test. Find a minimal compilable example reproducing the problem. You said you modified the view and the model. Can you reproduce the problem using a custom model with a standard view? How about a standard model and customized view?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop calling data() function for the child items in a tree view initially

    hmm.. I will try those things and let you know the result.
    mean while, with your experience do you have any idea or can you suspect any of the functions which might cause for this problem.

    Note :: I am doing some another thing in Data actually, Will this cause any problem ??

    Qt Code:
    1. QVariant Model::data( const QModelIndex &index, int role ) const
    2. {
    3. if ( ! index.isValid() )
    4. return QVariant();
    5.  
    6. Item *item = static_cast<Item*>( index.internalPointer() );
    7. switch( role )
    8. {
    9. case Qt::DisplayRole:
    10. {
    11.  
    12. //initially i am getting data which is needed for building empty tree, then i am populating that empty tree
    13. // on demand, when data function called I am reading data from Data base and updating Item data and then reading it
    14.  
    15.  
    16. 1. read data from data base //read one row from db
    17. 2. item->setData(data); // update that data to item
    18.  
    19. //then read ite
    20. return item->data( index.column(),Qt::DisplayRole );
    21. break;
    22. }
    23. }
    24. return QVariant();
    25. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop calling data() function for the child items in a tree view initially

    No, I don't think so.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop calling data() function for the child items in a tree view initially

    I could not found a reason for this problem, so I have implemented a logic

    1. if data is called for an item, I am checking weather it is child or not, if it is child item then I am checking its parent is expanded or not
    If it parent is expanded then I am firing a query to read a data from data base otherwise not firing a query as we are not showing it in a view (item is collapsed).
    2. when we expand an item, data function should be called then I am again I am checking parent is expanded or no, this time it will be true then I am firing a query and getting data.

    But strangely When I expand an item (Click on + sign ), data() function will not be calling at all :-(
    When I remove proxy model then data function is calling (when clicks on + sign).

    What is this strange behavior, why data function is not calling when proxy is there in above case ??


    Qt Code:
    1. QVariant TreeModel::data( const QModelIndex &index, int role ) const
    2. {
    3. if ( ! index.isValid() )
    4. return QVariant();
    5.  
    6. TreeItem *item = static_cast<TreeItem*>( index.internalPointer() );
    7. switch( role )
    8. {
    9. case Qt::DisplayRole:
    10. {
    11. TreeItem* l_parent = item->parent(); //parent item
    12.  
    13. bool l_getData = true;
    14.  
    15. if(m_rootItem == l_parent) // if top most item then we should read data
    16. {
    17. l_getData = true;
    18. }
    19. else if(l_parent && !l_parent->isExpanded()) //if its not top most item and its parent is not expanded, no need to read data
    20. {
    21. l_getData = false;
    22. }
    23.  
    24.  
    25. if(l_getData && !item->isDataUpdated()) // if data need to be read
    26. {
    27. ReadRowFromDb(index); // read from Data base & update item
    28. }
    29.  
    30. return item->data( index.column(),Qt::DisplayRole ); // then read data from item
    31. }
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to stop calling data() function for the child items in a tree view initially

    What is this strange behavior, why data function is not calling when proxy is there in above case ??
    Most likely you have not implemented the mapping functions in your proxy model, or you have implemented them incorrectly. Did you implement the data() method in the proxy?

  11. #11
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop calling data() function for the child items in a tree view initially

    The only function I have implemented in customized QSortFilterProxyModel is filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const.
    I have not implemented the data() function.

    What are the function I should to re implement while using proxy?

    Thanks in Advance.

  12. #12
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop calling data() function for the child items in a tree view initially

    I have implemented data() in proxy too, still same issue.



    Qt Code:
    1. QVariant proxy::data(const QModelIndex & proxy_index, int role) const
    2. {
    3. if ( ! index.isValid() )
    4. return QVariant();
    5.  
    6. QModelIndex source_index = mapToSource(proxy_index);
    7.  
    8. return QSortFilterProxyModel::data(proxy_index, role);
    9. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop calling data() function for the child items in a tree view initially

    Try minimizing your example instead of throwing in more and more code. Can you reproduce the issue with a bare standard item model and a bare QTreeView?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop calling data() function for the child items in a tree view initially

    When I tried using with QTreeView only, data function is calling as expected.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop calling data() function for the child items in a tree view initially

    Quote Originally Posted by DURGAPRASAD NEELAM View Post
    When I tried using with QTreeView only, data function is calling as expected.
    So please show your customizations to the view.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop calling data() function for the child items in a tree view initially

    Below is the code for view. (there is lot of other c++ code in view, but typical qt related stuff in view is this much)

    Qt Code:
    1. MyView::MyView ( Model* treeModel) : QTreeView(parent)
    2. {
    3. setContextMenuPolicy( Qt::DefaultContextMenu );
    4. setSelectionMode( QAbstractItemView::ExtendedSelection );
    5. setSelectionBehavior( QAbstractItemView::SelectItems );
    6. installEventFilter( this );
    7.  
    8. m_proxyTreeModel = new ProxyModel( this ); // proxy model:
    9. m_proxyTreeModel->setSourceModel( m_treeModel ); // tree model created in viewer:
    10. m_proxyTreeModel->setDynamicSortFilter(true);
    11. setModel( m_proxyTreeModel );
    12.  
    13. setAllColumnsShowFocus( true );
    14. setUniformRowHeights( true );
    15. }
    To copy to clipboard, switch view to plain text mode 

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop calling data() function for the child items in a tree view initially

    This is not your real code. This wouldn't even compile. Even if it did, it would crash when ran. And you were supposed to use a standard model against your custom view. Without any proxy models inbetween.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 14th March 2013, 23:50
  2. tree view displaying widgets as items
    By paksas in forum Qt Programming
    Replies: 9
    Last Post: 28th January 2013, 14:58
  3. Replies: 5
    Last Post: 17th February 2009, 05:35
  4. [QThread] Function calling after thread.stop()
    By Macok in forum Qt Programming
    Replies: 4
    Last Post: 7th February 2009, 14:33
  5. How did I disable auto-scrolling function in tree-view
    By alfa_wu in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2007, 09:31

Tags for this 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.