This is the header file for my tree item
class TreeItem
{
public:
explicit TreeItem( Container *data , TreeItem *parent = 0 );
~TreeItem();
TreeItem *parent();
void appendChild(TreeItem *child);
TreeItem *child(int iNumber);
int childCount() const;
int childNumber() const;
Container data() const ;
Container* GetContainer();
bool setData
(Container
* data ,
QVariant value
);
void setContainer( Container* data);
bool insertChildren(int position, int count );
bool removeChildren( int position , int count );
void removeChild(int row);
void removeChild(TreeItem* itm);
std::string getChildName(int row);
std::string getName();
int row() const;
void insertChild(int pos, TreeItem *child);
private:
QList<TreeItem*> childItems;
Container* itemData;
TreeItem* parentItem;
};
class TreeItem
{
public:
explicit TreeItem( Container *data , TreeItem *parent = 0 );
~TreeItem();
TreeItem *parent();
void appendChild(TreeItem *child);
TreeItem *child(int iNumber);
int childCount() const;
int childNumber() const;
Container data() const ;
Container* GetContainer();
bool setData(Container* data , QVariant value);
void setContainer( Container* data);
bool insertChildren(int position, int count );
bool removeChildren( int position , int count );
void removeChild(int row);
void removeChild(TreeItem* itm);
std::string getChildName(int row);
std::string getName();
int row() const;
void insertChild(int pos, TreeItem *child);
private:
QList<TreeItem*> childItems;
Container* itemData;
TreeItem* parentItem;
};
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.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool dropMimeData
(const QMimeData *mimeData, Qt
::DropAction action,
int row,
int column,
const QModelIndex &parent
) {
if (!mimeData->hasFormat(s_treeNodeMimeType)) {
return false;
}
QByteArray data
= mimeData
->data
(s_treeNodeMimeType
);
qint64 senderPid;
stream >> senderPid;
return false;
}
TreeItem *parentNode = getItem(parent);
int count;
stream >> count;
if (row == -1) {
if (parent.isValid())
row = 0;
else
row = rowCount(parent);
}
TreeItem *node;
for (int i = 0; i < count; ++i) {
qlonglong nodePtr;
stream >> nodePtr;
node = reinterpret_cast<TreeItem *>(nodePtr);
if (node->row() < row && parentNode == node->parent())
--row;
TreeItem *nodeNew = new TreeItem(node->GetContainer(), parentNode); // Create a new Data item using the node container's data.
beginInsertRows(parent, row, row);
parentNode->insertChild(row, nodeNew);
endInsertRows();
++row;
}
if (QGuiApplication::keyboardModifiers() != Qt::ControlModifier)
{
removeItem(node);
}
return true;
}
bool dropMimeData(const QMimeData *mimeData, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
if (!mimeData->hasFormat(s_treeNodeMimeType)) {
return false;
}
QByteArray data = mimeData->data(s_treeNodeMimeType);
QDataStream stream(&data, QIODevice::ReadOnly);
qint64 senderPid;
stream >> senderPid;
if (senderPid != QCoreApplication::applicationPid()) {
return false;
}
TreeItem *parentNode = getItem(parent);
int count;
stream >> count;
if (row == -1) {
if (parent.isValid())
row = 0;
else
row = rowCount(parent);
}
TreeItem *node;
for (int i = 0; i < count; ++i) {
qlonglong nodePtr;
stream >> nodePtr;
node = reinterpret_cast<TreeItem *>(nodePtr);
if (node->row() < row && parentNode == node->parent())
--row;
TreeItem *nodeNew = new TreeItem(node->GetContainer(), parentNode); // Create a new Data item using the node container's data.
beginInsertRows(parent, row, row);
parentNode->insertChild(row, nodeNew);
endInsertRows();
++row;
}
if (QGuiApplication::keyboardModifiers() != Qt::ControlModifier)
{
removeItem(node);
}
return true;
}
To copy to clipboard, switch view to plain text mode
Bookmarks