PDA

View Full Version : Drag Drop Help pls



munna
24th July 2006, 14:14
Hi,

I have started to implement a drag n drop operation from QListWidget to QTreeWidget. I have implemented the drag operation but it is not working. Here is my code.



void NameListWidget::mousePressEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton){
startPos = event->pos();
}
QListWidget::mousePressEvent(event);
}

void NameListWidget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton){
int xDiff = startPos.x() - event->pos().x();

if(xDiff > 3 ){//drag only if the movement is towards left
startDrag();
}
}
QListWidget::mouseMoveEvent(event);
}

void NameListWidget::startDrag()
{
QListWidgetItem * items = currentItem();
if(item){
QMimeData *mimeData = new QMimeData;
mimeData->setText(item->text());
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(QPixmap(":/images/personsmall.png"));
drag->setHotSpot(QPoint(drag->pixmap().width()/2,
drag->pixmap().height()));
drag->start();
}
}


When I start dragging the pixmap does not appear. Can someone please tell what can be the problem ?

Thanks a lot.

wysota
24th July 2006, 14:59
But the drag itself is fine? Can you make a successfull drop with it? Is the pixmap accessible?

munna
24th July 2006, 15:11
The pixmap is accessible and is right. I have not yet implemented the drop. I first wanted to check if the drag works fine. When I try to drag I get the forbiddeb cursor.

Any idea?

Thanks a lot.

wysota
24th July 2006, 16:08
Well... in my opinion the code looks fine. What happens if you resign from reimplementing startDrag() and encode the mime data through QListWidget::mimeData()? You should get a proper drag with a picture of your items as a pixmap. Does that work? Have you confirmed that the pixmap is there in the resource by loading it onto a QLabel or something like that?

munna
4th September 2006, 09:44
Sorry for getting back to this so late, but I have not yet got this working. Can someone please help


What happens if you resign from reimplementing startDrag() and encode the mime data through QListWidget::mimeData()?

Can you tell me more on this ?


Have you confirmed that the pixmap is there in the resource by loading it onto a QLabel or something like that?

Yes, I have confirmed. I use the same pixmap in couple of other places in my application. It appears in the other places.

Please Help
Thanks a lot

wysota
4th September 2006, 10:51
I meant that you should try something simpler, more straightforward. If you teach your widget to encode items according to a specific mime (for example the text you are using now) and teach your tree widget to accept those, you should get a working drag&drop. You can then experiment more with startDrag and custom pixmaps.

munna
4th September 2006, 11:25
I added the line setDragEnabled(true) in the constructor of ListWidget, commented my code for dragging and it kind of works. But I am looking for something different.

When I start dragging, I get forbidden cursor when the drag is still inside the QListWidget. When the drag is inside the QTreeWidget is shows correctly (both icon and text). QTreeWidget accepts the drop and the item is added as a top level item.

I would like to show what is being dragged while inside the ListWidget also. Apart from that I do not want a new top level item being added to the treewidget when dropped but something else.

Forget about the drop we can see that later.


If you teach your widget to encode items according to a specific mime (for example the text you are using now) and teach your tree widget to accept those, you should get a working drag&drop.

Can you please give a simple example ?

Thanks a lot.

wysota
4th September 2006, 12:11
I would like to show what is being dragged while inside the ListWidget also.
Make your list items return flag Qt::ItemIsDropEnabled and set acceptDrops of the widget to true.

Apart from that I do not want a new top level item being added to the treewidget when dropped but something else.
I'm not sure how to do that in Q*Widgets... using the model approach you should reimplement dropMimeData() and implement desired functionality there.