Search code examples
qtdrag-and-droppyqt4user-dataqstandarditem

QStandardItem with user data


In my app I have a QListView with a QStandardItemModel. I need the items to hold some user data, which works OK by extending QStandardItem and adding my own data. But I also want it possible to reorder items in the list by dragging and here the problem starts. It seems that QT clones the item when dropping it, copies the test and color, but ofc it doesn't know about my user data. I tried solving it in clone(), but it is only called with the item prototype (which is an empty item). I also tried overriding the setData() function and use the role UserRole for my user data, but that is not copied either when dragging.

Does anyone have a working example of something similar?


Solution

  • I finally had some time to work on this project again and found the solution. I had overridden setData to make sure the text and a regex pattern got updated when the user data was set, only that when doing drag and drop move, the item is cloned without calling this function for my data. I guess the implementation just copies the members directly. So my solution was to listen to move events on the list and then update everything needed on the new item.