Drag 'n Drop QListWidget: no text while dragging if not default drag used
I have a little problem with DnD using a QListWidget.
I have to create my own QListWidget class (called CListWidget) as I need to implement my own mousePressEvent method.
Code:
{
if ( currentItem()->text().isEmpty() )
{
clearSelection();
return;
}
dataStream << currentItem()->text();
mimeData->setData("application/x-dnditemdata", itemData);
mimeData->setText(currentItem()->text());
drag->setMimeData(mimeData);
drag->exec(Qt::CopyAction);
}
When using this code, I cannot see any text while dragging.
If I comment the code and just enable the drag:
Code:
{
setDragEnabled(true);
}
the text of the CListWidget item is shown while dragging.
Why ?
The problem is also that, if I use the QT default drag, I don't have any text in the item dragged.
Thanks for any suggestion :)
PS: Using Qt version 4.4.0 under Linux Ubuntu 8.04
Re: Drag 'n Drop QListWidget: no text while dragging if not default drag used
You will need to set pixmap in mime data to see the text while dragging...
From the docs -
Code:
{
QModelIndexList indexes = d->selectedDraggableIndexes();
if (indexes.count() > 0) {
QMimeData *data
= d
->model
->mimeData
(indexes
);
if (!data)
return;
QPixmap pixmap
= d
->renderToPixmap
(indexes,
&rect
);
// HERE THEY RENDER TEXT TO PIXMAP rect.adjust(horizontalOffset(), verticalOffset(), 0, 0);
drag->setPixmap(pixmap); // THE PIXMAP FOR SHOWING TEXT
drag->setMimeData(data);
drag->setHotSpot(d->pressedPosition - rect.topLeft());
Qt::DropAction defaultDropAction = Qt::IgnoreAction;
if (d->defaultDropAction != Qt::IgnoreAction && (supportedActions & d->defaultDropAction))
defaultDropAction = d->defaultDropAction;
else if (supportedActions
& Qt
::CopyAction && dragDropMode
() != QAbstractItemView::InternalMove) defaultDropAction = Qt::CopyAction;
if (drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction)
d->clearOrRemove();
}
}
Re: Drag 'n Drop QListWidget: no text while dragging if not default drag used
Thanks but I don't follow you...
I guess I have to reimplement the virtual method startDrag in my CListWidget class, but I get errors.
Re: Drag 'n Drop QListWidget: no text while dragging if not default drag used
Well, I've solved in this way:
added the following to the constructor:
Code:
m_Painter->setPen(pen);
and the mousePressEvent is now:
Code:
{
if ( currentItem()->text().isEmpty() )
{
clearSelection();
return;
}
dataStream << currentItem()->text();
m_Painter
->drawText
(QRect(10,
0,
170,
20), currentItem
()->text
() );
// ADDED
mimeData->setData("application/x-dnditemdata", itemData);
mimeData->setText(currentItem()->text());
drag->setPixmap(*m_Pixmap); // ADDED
drag->setMimeData(mimeData);
drag->exec(Qt::CopyAction);
}