jiveaxe
11th April 2014, 15:01
Hi, I sub-classed QListWidget to handle drag&drop. This new object should handle both internal move and item copy when drag&drop between qlistwidgets. I achieved the latter (copy on drop) but internal move is a problem: I'm unable to get it right. This is my implementation:
MyListWidget::MyListWidget(QWidget *parent) :
QListWidget(parent)
{
setAlternatingRowColors(true);
setDragEnabled(true);
setDragDropMode(QAbstractItemView::InternalMove);
setDropIndicatorShown(true);
setAcceptDrops(true);
}
void MyListWidget::dragEnterEvent(QDragEnterEvent *event)
{
if(event->source() == this) {
QListWidget::dragEnterEvent(event);
} else if (event->mimeData()->hasFormat("application/x-myapp")) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
void MyListWidget::dragMoveEvent(QDragMoveEvent *event)
{
if(event->source() == this) {
QListWidget::dragMoveEvent(event);
} else if (event->mimeData()->hasFormat("application/x-myapp")) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
void MyListWidget::dropEvent(QDropEvent *event)
{
if (event->source() == this) {
QListWidget::dropEvent(event);
} else if (event->mimeData()->hasFormat("application/x-myapp")) {
QByteArray itemData = event->mimeData()->data("application/x-myapp");
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
People people;
dataStream >> people;
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
PeopleItem *peopleItem = new PeopleItem(people, this);
QListWidgetItem *wItem = new QListWidgetItem;
addItem(wItem);
setItemWidget(wItem, peopleItem);
wItem->setSizeHint(QSize(40,40));
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
void MyListWidget::mousePressEvent(QMouseEvent *event)
{
People people = ((PeopleItem *)itemWidget(itemAt(event->pos())))->people();
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << people;
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-myapp", itemData);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->exec(Qt::CopyAction);
}
Where's my error?
Thanks,
MyListWidget::MyListWidget(QWidget *parent) :
QListWidget(parent)
{
setAlternatingRowColors(true);
setDragEnabled(true);
setDragDropMode(QAbstractItemView::InternalMove);
setDropIndicatorShown(true);
setAcceptDrops(true);
}
void MyListWidget::dragEnterEvent(QDragEnterEvent *event)
{
if(event->source() == this) {
QListWidget::dragEnterEvent(event);
} else if (event->mimeData()->hasFormat("application/x-myapp")) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
void MyListWidget::dragMoveEvent(QDragMoveEvent *event)
{
if(event->source() == this) {
QListWidget::dragMoveEvent(event);
} else if (event->mimeData()->hasFormat("application/x-myapp")) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
void MyListWidget::dropEvent(QDropEvent *event)
{
if (event->source() == this) {
QListWidget::dropEvent(event);
} else if (event->mimeData()->hasFormat("application/x-myapp")) {
QByteArray itemData = event->mimeData()->data("application/x-myapp");
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
People people;
dataStream >> people;
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
PeopleItem *peopleItem = new PeopleItem(people, this);
QListWidgetItem *wItem = new QListWidgetItem;
addItem(wItem);
setItemWidget(wItem, peopleItem);
wItem->setSizeHint(QSize(40,40));
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
void MyListWidget::mousePressEvent(QMouseEvent *event)
{
People people = ((PeopleItem *)itemWidget(itemAt(event->pos())))->people();
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << people;
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-myapp", itemData);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->exec(Qt::CopyAction);
}
Where's my error?
Thanks,