Results 1 to 3 of 3

Thread: How to move table view tree item along with its children with custom data.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2019
    Posts
    2
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default How to move table view tree item along with its children with custom data.

    This is the header file for my tree item


    Qt Code:
    1. class TreeItem
    2. {
    3. public:
    4. explicit TreeItem( Container *data , TreeItem *parent = 0 );
    5. ~TreeItem();
    6. TreeItem *parent();
    7. void appendChild(TreeItem *child);
    8.  
    9. TreeItem *child(int iNumber);
    10. int childCount() const;
    11. int childNumber() const;
    12. Container data() const ;
    13. Container* GetContainer();
    14. bool setData(Container* data , QVariant value);
    15. void setContainer( Container* data);
    16. bool insertChildren(int position, int count );
    17. bool removeChildren( int position , int count );
    18. void removeChild(int row);
    19. void removeChild(TreeItem* itm);
    20. std::string getChildName(int row);
    21. std::string getName();
    22. int row() const;
    23. void insertChild(int pos, TreeItem *child);
    24.  
    25. private:
    26. QList<TreeItem*> childItems;
    27. Container* itemData;
    28. TreeItem* parentItem;
    29. };
    To copy to clipboard, switch view to plain text mode 
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    Each tree item holds a pointer reference to a container object.

    when i move or copy( Cntrl Pressed) a single tree item i create a new instance of tree item and copy the data from the old item.

    Currently in my code i am able to do this for single item , how to do it for a item having multiple children.

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



    Qt Code:
    1. bool dropMimeData(const QMimeData *mimeData, Qt::DropAction action, int row, int column, const QModelIndex &parent)
    2. {
    3. if (!mimeData->hasFormat(s_treeNodeMimeType)) {
    4. return false;
    5. }
    6. QByteArray data = mimeData->data(s_treeNodeMimeType);
    7. QDataStream stream(&data, QIODevice::ReadOnly);
    8. qint64 senderPid;
    9. stream >> senderPid;
    10. if (senderPid != QCoreApplication::applicationPid()) {
    11. return false;
    12. }
    13. TreeItem *parentNode = getItem(parent);
    14. int count;
    15. stream >> count;
    16. if (row == -1) {
    17. if (parent.isValid())
    18. row = 0;
    19. else
    20. row = rowCount(parent);
    21. }
    22. TreeItem *node;
    23. for (int i = 0; i < count; ++i) {
    24. qlonglong nodePtr;
    25. stream >> nodePtr;
    26. node = reinterpret_cast<TreeItem *>(nodePtr);
    27. if (node->row() < row && parentNode == node->parent())
    28. --row;
    29.  
    30. TreeItem *nodeNew = new TreeItem(node->GetContainer(), parentNode); // Create a new Data item using the node container's data.
    31. beginInsertRows(parent, row, row);
    32. parentNode->insertChild(row, nodeNew);
    33. endInsertRows();
    34. ++row;
    35.  
    36. }
    37.  
    38. if (QGuiApplication::keyboardModifiers() != Qt::ControlModifier)
    39. {
    40. removeItem(node);
    41. }
    42. return true;
    43. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sumit kang; 14th August 2019 at 12:24.

Similar Threads

  1. Replies: 1
    Last Post: 31st March 2016, 10:51
  2. Replies: 3
    Last Post: 17th January 2016, 18:06
  3. Building Tree view from Table view which is already build.
    By DURGAPRASAD NEELAM in forum Newbie
    Replies: 6
    Last Post: 18th May 2015, 07:18
  4. Get data from table view after item was edited?
    By schmimona in forum Qt Programming
    Replies: 7
    Last Post: 8th September 2011, 12:47
  5. Help on adding children to tree view
    By vieraci in forum Qt Programming
    Replies: 3
    Last Post: 5th June 2009, 07:54

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.