Struggle to insert a QListWidgetItem at his old position ...
-
Hi everybody,
I have a QListWidget (Qt 5.15.16 MacOS - C++). I use Drag&Drop to reorder the list items.
I want to put a "moved" item back to its origin position if a certain condition is not matched.
I can Drag&Drop works, programatically "take" the item works also and gets removed.
But inserting the same item back at his origin row fails. Here is sample code:void MainWindow::slotListWidgetOrderChanged(const QModelIndex &, int from, int, const QModelIndex &, int to) { qDebug() << "ListWidget order changed from " << from << "to" << to; if(! mycondition) { //Put item back to origin location ... QListWidgetItem* myItem = ui->listWidget->takeItem(from); ui->listWidget->insertItem(from, myItem); return; } else { //Do something else .... } }
-
@ademmler said in Struggle to insert a QListWidgetItem at his old position ...:
But inserting the same item back at his origin row fails.
What fails about it?
Seems to me, that this moves nothing.
QListWidgetItem* myItem = ui->listWidget->takeItem(from); ui->listWidget->insertItem(from, myItem);
For example, with
from == 1
List widget items: A B C D
Take index 1: myItem = B
List widget items: A C D
Insert at index 1
List widget items: A B C D -
Hi,
Why not forbid the move if the condition is met ?
That way there's no surprise move of the item. -
Thx for your help.
@SGaist: How can I forbid a move, when the item has been moved already.
To explain: item 1,2,3 should not be moved, item 4,5,6,7 ... can be moved.@ChrisW67: I want to move the taken item back to its old position.
Isn't "from" the old position? Does the position be changed while "dragging" or afterwards?The aim is: User picks an element, which should not be moved. He drags it to a new position. Now this item should jump back to its old position. and finally the user gets a dialog telling him why this is not possible.
-
Take a look at this part of the model view programming documentation. It describes how to handle drag and drop.
-
@ademmler said in Struggle to insert a QListWidgetItem at his old position ...:
@ChrisW67: I want to move the taken item back to its old position.
Isn't "from" the old position? Does the position be changed while "dragging" or afterwards?I would guess it only changes after the drag/drop. But in any case these two lines
QListWidgetItem* myItem = ui->listWidget->takeItem(from); ui->listWidget->insertItem(from, myItem);
take an item out at
from
and immediately put it back in atfrom
so that's just a NOOP.