[Qt-interest] prevent a drop in QTreeWidget

Graham Labdon Graham.Labdon at avalonsciences.com
Fri May 27 12:29:48 CEST 2011


Hi
Fixed it by reimplementing dragMoveEvent - 

void MyTreeWidget::dragMoveEvent(QDragMoveEvent* event)
{
	QModelIndex index = indexAt(event->pos());
	QAbstractItemView::DropIndicatorPosition dropIndPosition =dropIndicatorPosition();

	bool acceptAction = false;
	switch (dropIndPosition)
	{
		case QAbstractItemView::OnItem:
		case QAbstractItemView::BelowItem:
		{
			if (index.row() >= 1 || index.row() == -1)
			{
				acceptAction = true;
			}
		}
		break;
		case QAbstractItemView::AboveItem:
		{
			if (index.row() >= 2)
			{
				acceptAction = true;
			}
		}
		break;
		case QAbstractItemView::OnViewport:
		{
			acceptAction = true;
		}
		break;
		default:
		{
			acceptAction = false;
		}
		break;
	}
	if (acceptAction)
	{
		QAbstractItemView::dragMoveEvent(event);
	}
	else
	{
		event->ignore();
	}
}

Thanks for your help

-----Original Message-----
From: Ross Bencina [mailto:rossb-lists at audiomulch.com] 
Sent: 27 May 2011 11:22
To: Graham Labdon; qt-interest at qt.nokia.com
Subject: Re: [Qt-interest] prevent a drop in QTreeWidget

Graham Labdon wrote:
if (index.row() > 1 || (index.row() == 1 && dropIndPosition == 
QAbstractItemView::BelowItem))
{
QAbstractItemView::dropEvent(event);
}
else
{
event->setDropAction(Qt::IgnoreAction);
event->accept();
}
}

> The problems I have are
> 1. I cannot move an item to the bottom of the tree (in this case 
> dropIndicatorPosition is OnViewport)

Well you'll have to allow OnViewport in the first branch of your if 
statement then. You could check the pos().x() to know if you're above or 
below the items (I think I get the top of the first item using 
QAbstractItemView::visualRect  and compare the event pos.x against that).


> 2. If an item has subitems - when I move the item the subitems are removed

That, I'm afraid, I know nothing about. I'm not sure if the default 
drag-and-drop mechanism supports deep copying of items. You might want to 
post a new question and give details of the Model implementation you're 
using.

Ross. 




More information about the Qt-interest-old mailing list