[Qt-interest] prevent a drop in QTreeWidget

Graham Labdon Graham.Labdon at avalonsciences.com
Fri May 27 11:17:30 CEST 2011


Hi Ross

Thanks for the info

I have something working but still have some problems 
Here is my implementation of dropEvent

void MyTreeWidget::dropEvent(QDropEvent* event)
{
	qDebug() << "MyTreeWidget::dropEvent";


	QModelIndex index = indexAt(event->pos());

	qDebug() << "Index : " << index.column() << " " << index.row();

	QAbstractItemView::DropIndicatorPosition dropIndPosition = dropIndicatorPosition();

	qDebug() << "p : " << dropIndPosition;

	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)
2. If an item has subitems - when I move the item the subitems are removed

Have you any suggestions for this?

Thanks

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

Graham Labdon wrote:
> How do I get the information that tells me where the item is being dropped 
> so I can tell whether or not the drop should be allowed?

Have you looked at the QDropEvent docs?

http://doc.qt.nokia.com/latest/qdropevent.html

QDropEvent::pos() seems like a likely contender.

You might want to use this in conjunction with QTreeWidget::itemAt() for 
example.

QAbstractItemView::dropIndicatorPosition () is also interesting.

I guess you've read this:
http://doc.qt.nokia.com/latest/model-view-programming.html#using-drag-and-drop-with-item-views

Ross. 




More information about the Qt-interest-old mailing list