PDA

View Full Version : QTReeWidget Drag and Drop



sujan.dasmahapatra
18th March 2011, 10:36
Dear Friends
I am trying to implement drag and drop for QTreeWidgetItem.I have some toplevelitems and some of them have children items. I want to implement hte drag and drop functionality for the toplevel items, when an item if dragged and dropped the toplevele items and its children items all should occupy the new place. Somekind of reordering I want to achieve. Please give me some guidelines.
I am curreny trying to implement startDrag() but its crashing.

void Tree::startDrag(Qt::DropActions supportedActions)
{
QDrag *drag = new QDrag(this);
QMimeData *data = new QMimeData;
data->setText(currentItem()->text(0));
drag->setMimeData(data);
drag->exec(supportedActions, Qt::MoveAction);
}

Thanks in advance

camelsoft
18th March 2011, 15:54
I've tried to do something like that recently. In mousePressEvent() function execute a new drag object and
in dropEvent() function handle with replacing the item.
Following code may help.



void Tree::dragMoveEvent(QDragMoveEvent * event) {
event->acceptProposedAction();
}

void Tree::dragEnterEvent(QDragEnterEvent * event ) {
event->acceptProposedAction();

}

void Tree::dropEvent(QDropEvent * event ) {
dropingOn = this->itemAt(event->pos());
int dropingIndex = this->indexOfTopLevelItem(dropingOn);
this->takeTopLevelItem(this->indexOfTopLevelItem(draggingItem));
int index = this->indexOfTopLevelItem(dropingOn);
if(index < dropingIndex) index++;
this->insertTopLevelItem(index, draggingItem);

}

void Tree::mousePressEvent(QMouseEvent *event) {
if(event->button()==Qt::LeftButton) {
draggingItem = this->itemAt(event->pos());
QDrag *drag = new QDrag(this);
drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction);
event->accept();
}
}

sujan.dasmahapatra
21st March 2011, 09:00
hi camelsoft your suggestion works fine but I am getting one problem, that I am not able to selecte any item in the treeWidget. it seems its disabled. what can be done for that. Thanks sujan

Added after 30 minutes:

Dear Friends
I am implementing drag and drop for treeWidget. I am able to do it. But as I implement mousePressEvent() I am not able to expand or select items from the treeview as I have childitems for every toplevelitem.
Whats going wrong in this. Please tell me someone.


QTreeWidgetItem * draggingItem = new QTreeWidgetItem(this);

void Tree::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
draggingItem = this->itemAt(event->pos());
QDrag *drag = new QDrag(this);
drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction);
event->accept();
}
}

camelsoft
23rd March 2011, 09:14
in the constructor of your tree widget implementation add these lines:


this->setDragDropMode(QAbstractItemView::InternalMove);
this->setSelectionMode(QAbstractItemView::SingleSelectio n);
this->setDragEnabled(true);
this->setAcceptDrops(true);

sujan.dasmahapatra
24th March 2011, 06:04
Hi camelsoft,
mousePressEvent is actually hindering QTreeWidget's normal functions, like selecvt an item, expand child indicator etc, so plss suggest some other way of dragging an item, like can aI use startDrag() instead of mousPressEvent... Thanks sujan

krsree
2nd June 2013, 16:15
Hii

If the Source and Destniation TreeWidgets are two seperate classes, How drag and drop will achieve.. Any sample, please upload

Sree