@Christian-Ehrlicher So i got this working pretty great, but somehow when i move 2 items at ones out of it's parent only the data of one item gets copied over to both new inserted items:
This i the start situation (1 parent with 2 child items):
[image: 7af7df79-973a-4e1d-ae2a-a21aebd2be91.png]
Than i select both items and drop than onto their parent item:
[image: 0b7bbbd0-3700-4b67-9d56-b3ea8d06d023.png]
After dropping i get two items named identically, but instead they should keep their data:
[image: 8fe0b267-976b-43d6-81e0-7d0c679fa1ad.png]
Here is my code:
void ViewLayerList::dragEnterEvent(QDragEnterEvent *event)
{
// Ermitteln Sie den Index des Items unter der Maus
QModelIndex index = indexAt(event->position().toPoint());
if (!index.isValid()) {
// Wenn kein gültiges Item unter der Maus ist, rufen Sie die Basisklasse-Methode auf und kehren Sie zurück
QTreeView::dragEnterEvent(event);
return;
}
// Zugriff auf das Modell
QStandardItemModel *modelObj = dynamic_cast<QStandardItemModel*>(model);
// Überprüfen Sie, ob das Modell korrekt gecastet wurde
if (modelObj)
{
// Vor jedem Drag-Event leeren Sie die Parent-Index-Liste
parentChildMap.clear();
selectedIndexList.clear();
// Sammeln Sie die Parent-Indizes für ausgewählte Items
QModelIndexList selectedIndexes = selectionModel()->selectedIndexes();
for (const QModelIndex& selectedIndex : selectedIndexes)
{
QModelIndex parentIndex = selectedIndex.parent();
// Fügen Sie das Paar zur Map hinzu
parentChildMap.insert(selectedIndex, parentIndex);
// Fügen Sie den ausgewählten Index zur Liste hinzu
selectedIndexList.append(selectedIndex);
// Zugriff auf das ausgewählte Element und fügen Sie es zur Liste hinzu
QStandardItem* selectedItem = modelObj->itemFromIndex(selectedIndex);
}
int selectedItemCount = selectedIndexes.count();
qDebug() << "Anzahl der ausgewählten Items: " << selectedItemCount;
// Nachdem Sie die Map gefüllt haben...
for(auto it = parentChildMap.constBegin(); it != parentChildMap.constEnd(); ++it)
{
qDebug() << "Child: " << it.key().data()
<< ", Parent: " << it.value().data();
}
}
QTreeView::dragEnterEvent(event);
}
void ViewLayerList::dropEvent(QDropEvent *event)
{
// Ermitteln Sie den Index des Items unter der Maus
QModelIndex index = indexAt(event->position().toPoint());
if (!index.isValid()) {
QTreeView::dropEvent(event);
return;
}
QModelIndex destinationIndex = indexAt(event->position().toPoint());
// Rufen Sie die Basisklasse-Methode auf, um die Standard-Verarbeitung fortzusetzen
QTreeView::dropEvent(event);
// Zugriff auf das Modell
QStandardItemModel *modelObj = dynamic_cast<QStandardItemModel*>(this->model);
// Überprüfen Sie, ob das Modell korrekt gecastet wurde
if(modelObj)
{
qDebug() << "destinationIndex data: " << destinationIndex.data();
// Überprüfen Sie, ob der Zielindex das Wurzelelement als Elternteil hat
bool isDestinationRoot = (destinationIndex.parent() == QModelIndex());
qDebug() << "DestinationHasROOT as parent: " << isDestinationRoot;
// Nachdem Sie das Ereignis verarbeitet haben...
for (const QModelIndex& selectedIndex : selectedIndexList)
{
// Holen Sie sich den Elternindex für das ausgewählte Element
QModelIndex parentIndex = parentChildMap.value(selectedIndex);
// Zugriff auf das Elternelement
QStandardItem* parentItem = modelObj->itemFromIndex(parentIndex);
// Vergleichen Sie die Daten des Elternindex mit den Daten des Zielindex
if(parentIndex.data() == destinationIndex.data() && isDestinationRoot) {
// Entfernen Sie das Kind-Element aus seinem Elternelement
int row = 0;
QList<QStandardItem*> items = parentItem->takeRow(row);
//THIS COMMET BEHAVES REALLY STRANGE WHEN I REMOVE IT
//SEE THE PICTURE BELOW
qDebug() << "ItemList:" << parentItem->takeRow(row);
// Bestimmen Sie die Position, an die das Element verschoben werden soll
int newPosition = parentIndex.row() + 1;
// Fügen Sie das Kind-Element an der neuen Position zum invisibleRootItem hinzu
modelObj->invisibleRootItem()->insertRow(newPosition, items);
}
}
}
}
There also is this one comment, everytime i try to remove it,my items get only copied in the dragging process but not removed anymore, so i end up with copies like this: (if the comment is there everything works fine, i found that strange)
[image: bb1f24a2-433c-49f4-974b-9a6a90c802a1.png]
I only commented out the comment:
//qDebug() << "ItemList:" << parentItem->takeRow(row);