So, finally I've do my first idea. Here it's the code, it could help someone one day!
void DragNDropQListWidget::startDrag(Qt::DropActions) {
int row = currentRow();
//We put the current row into datastream
dataStream << row;
mimeData->setData("application/x-dndgroup", itemData);
drag->setMimeData(mimeData);
drag->exec(Qt::MoveAction);
}
void DragNDropQListWidget
::dropEvent(QDropEvent *event
) { if (event->mimeData()->hasFormat("application/x-dndgroup")) {
QByteArray itemData
= event
->mimeData
()->data
("application/x-dndgroup");
int previousRow;
//Will stock the target destination row
int currentDropRow = row(itemAt(event->pos()));
//We take the source row
dataStream >> previousRow;
//Beforhand, we've stock the itemWidget into a QList of QWidget*
wA->setLayout(vLayoutA);
QVBoxLayout *vLayoutTemp
= refWidget
->takeAt
(previousRow
);
refWidget->insert(currentDropRow, vLayoutTemp);
//We "remove" the item of the list
itemToDrag = takeItem(previousRow);
//Important, we have to remove the item widget, otherwise it will be blank widget.
removeItemWidget(itemToDrag);
//We insert the removed item into the destination
insertItem(currentDropRow, itemToDrag);
//And we reset the item widget.
setItemWidget(itemToDrag, wA);
setCurrentItem(itemToDrag);
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->ignore();
}
} else {
event->ignore();
}
}
void DragNDropQListWidget::startDrag(Qt::DropActions) {
int row = currentRow();
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
//We put the current row into datastream
dataStream << row;
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-dndgroup", itemData);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->exec(Qt::MoveAction);
}
void DragNDropQListWidget::dropEvent(QDropEvent *event) {
if (event->mimeData()->hasFormat("application/x-dndgroup")) {
QListWidgetItem *itemToDrag;
QByteArray itemData = event->mimeData()->data("application/x-dndgroup");
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
int previousRow;
//Will stock the target destination row
int currentDropRow = row(itemAt(event->pos()));
//We take the source row
dataStream >> previousRow;
//Beforhand, we've stock the itemWidget into a QList of QWidget*
QVBoxLayout *vLayoutA = refWidget->at(previousRow);
QWidget *wA = new QWidget;
wA->setLayout(vLayoutA);
QVBoxLayout *vLayoutTemp = refWidget->takeAt(previousRow);
refWidget->insert(currentDropRow, vLayoutTemp);
//We "remove" the item of the list
itemToDrag = takeItem(previousRow);
//Important, we have to remove the item widget, otherwise it will be blank widget.
removeItemWidget(itemToDrag);
//We insert the removed item into the destination
insertItem(currentDropRow, itemToDrag);
//And we reset the item widget.
setItemWidget(itemToDrag, wA);
setCurrentItem(itemToDrag);
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->ignore();
}
} else {
event->ignore();
}
}
To copy to clipboard, switch view to plain text mode
For buttons who up and down items, use the same idea (save the widget created into a QList).
Bookmarks