I've suspected rendering bug I've seen recently, but trying your example I can tell that's not it.
Also in my case the issue was present on both windows and linux box.
As you took effort to provide woking example of the issue I'll help you.
Here's working piece of code based on your example (it supports moving top level items as well as children within the same top level item):
CTreeWidget
::CTreeWidget( QWidget* parent
) :
draggedItem( NULL )
{
setDragEnabled( true );
setDropIndicatorShown( true );
}
{
this->draggedItem = this->itemAt( e->pos() );
}
{
this->draggedItem = NULL;
}
{
e->acceptProposedAction();
}
{
if( this->draggedItem->parent() == this->itemAt( e->pos() )->parent() )
{
e->acceptProposedAction();
}
else
{
e->setDropAction( Qt::IgnoreAction );
}
}
{
event->setDropAction( Qt::IgnoreAction );
if( node )
{
if( node != droppedItem->parent() )
{
// not supported, ingoring action in dragMoveEvent() should be enough but better safe than sorry!
return;
}
int draggedIdx = node->indexOfChild( this->draggedItem );
int droppedIdx = node->indexOfChild( droppedItem );
node->insertChild( droppedIdx, node->takeChild( draggedIdx ) );
}
else
{
int draggedIdx = this->indexOfTopLevelItem( this->draggedItem );
int droppedIdx = this->indexOfTopLevelItem( droppedItem );
if( draggedIdx <= -1 || droppedIdx <= -1 )
{
// can't do
return;
}
this->insertTopLevelItem( droppedIdx, this->takeTopLevelItem( draggedIdx ) );
}
this->draggedItem = NULL;
}
CTreeWidget::CTreeWidget( QWidget* parent )
:
QTreeWidget( parent ),
draggedItem( NULL )
{
setDragEnabled( true );
setDropIndicatorShown( true );
setDragDropMode( QAbstractItemView::InternalMove );
setSelectionMode( QAbstractItemView::SingleSelection );
}
void CTreeWidget::mousePressEvent( QMouseEvent* e )
{
this->draggedItem = this->itemAt( e->pos() );
QTreeWidget::mousePressEvent( e );
}
void CTreeWidget::mouseReleaseEvent( QMouseEvent* e )
{
QTreeWidget::mouseReleaseEvent( e );
this->draggedItem = NULL;
}
void CTreeWidget::dragEnterEvent( QDragEnterEvent* e )
{
e->acceptProposedAction();
QTreeWidget::dragEnterEvent( e );
}
void CTreeWidget::dragMoveEvent( QDragMoveEvent* e )
{
if( this->draggedItem->parent() == this->itemAt( e->pos() )->parent() )
{
e->acceptProposedAction();
}
else
{
e->setDropAction( Qt::IgnoreAction );
}
QTreeWidget::dragMoveEvent( e );
}
void CTreeWidget::dropEvent(QDropEvent * event )
{
event->setDropAction( Qt::IgnoreAction );
QTreeWidgetItem* node = this->draggedItem->parent();
QTreeWidgetItem* droppedItem = this->itemAt( event->pos() );
if( node )
{
if( node != droppedItem->parent() )
{
// not supported, ingoring action in dragMoveEvent() should be enough but better safe than sorry!
return;
}
int draggedIdx = node->indexOfChild( this->draggedItem );
int droppedIdx = node->indexOfChild( droppedItem );
node->insertChild( droppedIdx, node->takeChild( draggedIdx ) );
}
else
{
int draggedIdx = this->indexOfTopLevelItem( this->draggedItem );
int droppedIdx = this->indexOfTopLevelItem( droppedItem );
if( draggedIdx <= -1 || droppedIdx <= -1 )
{
// can't do
return;
}
this->insertTopLevelItem( droppedIdx, this->takeTopLevelItem( draggedIdx ) );
}
this->draggedItem = NULL;
}
To copy to clipboard, switch view to plain text mode
I didn't spent time to look for what exacly was the problem, but it was a problem with your code not windows/linux box.
I hope that will solve your problems.
Cheers!
Bookmarks