PDA

View Full Version : QListWidgetItem in Drag n' drop



estanisgeyer
5th October 2008, 17:23
Hi friends,

I have a window of configuration of the toolbar on my application, running smoothly the drag n 'drop the items.
I'm using QListWidgetItem with icons, but when you drag the item to another widget, the icon does not go with him. How can I improve this follow code in QMimeData?



void ListAct::performDrag()
{
QListWidgetItem *item = currentItem();
if (item)
{
QMimeData *mime = new QMimeData;
mime->setText(item->text());

QDrag *drag = new QDrag(this);
drag->setMimeData(mime);

if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
delete item;
}
}


Thanks,

Marcelo E. Geyer
Brazil/RS

jpn
6th October 2008, 08:42
There is no need to implement DND by hand. It is already supported by the model view framework. See Using Drag and Drop with Item Views - Using Convenience Views (http://doc.trolltech.com/4.4/model-view-dnd.html#using-convenience-views) for more details.

estanisgeyer
7th October 2008, 21:08
Hi,

Ok, I solved and now my items are moved between QListWidgets subclass. See the code below.
I need is that when drag the item "Separator" (see the image) to another QListWidget subclass, the action is to copy (I believe that use Qt:: CopyAction) and not to move. To do this a few times I tried to write code but unsuccessful, reimplementation dropEvent. What happens and that by dragging up the other QListWidget, nothing happens, he does not copy the item.
Based on the code that I have, what and how should I reimplement?



ListAct::ListAct(QWidget *parent)
:QListWidget(parent)
{
setViewMode(QListView::ListMode);
setSelectionMode(QAbstractItemView::SingleSelectio n);
setDragEnabled(TRUE);
viewport()->setAcceptDrops(TRUE);
setDropIndicatorShown(TRUE);
setMovement(QListView::Snap);
setUniformItemSizes(TRUE);
setDragDropMode(QAbstractItemView::DragDrop);
}

ListAct::~ListAct()
{

}

void ListAct::addAct()
{
QListWidgetItem *sep = new QListWidgetItem(this);
sep->setText(QString::fromUtf8("--- Separator ---"));
sep->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);

QListWidgetItem *item = new QListWidgetItem(this);
item->setText(QString::fromUtf8("ItemTest1"));
item->setIcon(QIcon(":/imagens/clients.png"));
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);

QListWidgetItem *item2 = new QListWidgetItem(this);
item2->setText(QString::fromUtf8("ItemTest2"));
item2->setIcon(QIcon(":/imagens/clients.png"));
item2->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
}


Qt::DropActions ListAct::supportedDropActions() const
{
return Qt::MoveAction; // | Qt::CopyAction
}


Thanks,
Marcelo E. Geyer

estanisgeyer
2nd April 2009, 17:02
Hi friends,

Posted this a while ago and now get back to this code. Can you help it? On this my last question posted?

Thanks,

Marcelo E. Geyer.

estanisgeyer
3rd April 2009, 15:43
Nobody can help me solve this?
Thanks,

Marcelo E. Geyer