PDA

View Full Version : drag and drop disappear the item for windows, in Linux it fine



sujan.dasmahapatra
19th January 2012, 07:43
Dear Friends
In my application I have implemented drag and drop on the treewidget items. In linux its working fine but in windows I see when dragging and dropping an item in the treewidget list, then from where the item is dragged there’s items name is disappearing but its child items are present. And the item become inactive. Its working fine in Linux but in windows I am facing this problem. Please give me some suggestions. Check my drag and drop event.



void TreeWidget::dragMoveEvent(QDragMoveEvent * event)
{
event->acceptProposedAction();
}
void TreeWidget::dragEnterEvent(QDragEnterEvent * event )
{
Preprocessor = true;
draggingItem = currentItem();
event->acceptProposedAction();
oldindex = indexOfTopLevelItem(currentItem());
}
void TreeWidget::dropEvent(QDropEvent * event )
{
dropingOn = this->itemAt(event->pos());
if(!dropingOn)
return;
if(dropingOn->parent())
return;
int dropingIndex = this->indexOfTopLevelItem(dropingOn);
this->takeTopLevelItem(this->indexOfTopLevelItem(draggingItem));
index = this->indexOfTopLevelItem(dropingOn);
if(index < dropingIndex) index++;
this->insertTopLevelItem(index, draggingItem);
}

:)

Spitfire
19th January 2012, 17:21
Run the app on linux with -graphicssystem raster and see if the same thing happens like on windows.

sujan.dasmahapatra
20th January 2012, 06:52
no its not happening i ran it like this


bin/AirfoilOptGUI -graphicssystem raster

where AirfoilOptGUI is the application exe.

no problem in LINUX. do u have any idea what could be the problem ???

sujan.dasmahapatra
20th January 2012, 12:44
Dear Friends
There’s one project is there attachment. Just load and compile the application.
1. Click on the menu ‘load’ under ‘TreeReOrder’
2. It’ll laod the tree
3. Then drag any item from its position to some other position. It’s supposed to insert the item at the new position. Its inserting but the item in the original position missing its text. No text is there.

Please help me to resolve this problem. How can I get the drag and drop of topLevelItems from one position to another position. Corresponding children item should also move with the parent item.

The problem is happening in windows, in linux its working fine. Please check if you could give me some solution. Thanks Sujan

Spitfire
20th January 2012, 17:21
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 )
:
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;
}
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!

sujan.dasmahapatra
21st January 2012, 13:47
Thanks a lot spitfire I am checking and executing your piece of code now I'll let know if it works according to my need. But to tell you one thing that my code is working fine in Linux, if you execute this code in windows you can seee that after drag and drop the item from one place to another the text of the last item is lost. Just execute and see. If you could be able to correct my code to handle specially in windows it'll be more helpful. Thanks a lot for your support. Thanks Sujan

sujan.dasmahapatra
23rd January 2012, 08:40
Its working fine spitfire. I am handling my application in windos with your code. There's no need of mousePressEvent and mouseReleaseEvent even without them it's working fine. Thanks a lot for your help . Sujan

Spitfire
24th January 2012, 12:29
And as I mentioned in my post above - I've seen your problem on both linux and windows box so in my case your code didn't work correctly on neither platform.

As to the mouse press/release events. I don't know how it can work for you without press event handler (unless you've made some changes and you get the dragged element from somewhere else, but I think the correct place to get it is the mouse press event).

Anyway, I'm glad I could help.