PDA

View Full Version : Problem with subclassed QListWidgetItem by using drag 'n drop



Schreihals
25th October 2010, 08:56
Hey,

i've got some problems with my implementation of QListWidgetItem. I added some members, but after dragging them to another qlistwidget they've "lost" their members.

i set the type manually to a custom type (like 12345) and if i get the type in my qlistwidget (also subclassed), the type of the dropped item has changed to 0 (therefore my problem exists in dragging and dropping?!), only the name() member is transfered.

for drag and drop i use the "original" functions from qt.

can anybody help me?

verigySupport
17th December 2010, 02:16
If by "adding some members" to QListWidgetItem you mean you derived a new class from it, then the problem is that drag and drop does not know about your new class. When you start the drag the class is downgraded to QListWidgetItem.

Adding a new MIME type for drag and drop is possible, but I haven't mastered it yet.

The solution I have used it to add user roles to the standard QListWidgetItem and store the data using QVariants. In this way a new class is not needed and drag and drop works correctly. This includes dragging an item from one list to another, and
moving an item within the same list using drag/drop.

enum { MyIntValue = Qt::UserRole+0, MyStringValue = Qt::UserRole+1 };
QListWidgetItem* item = new QListWidgetItem("foo");
item->setData(MyIntValue,1234);
item->setData(MyStringValue,"hello");
myList->addItem(item);

later:

QListWidgetItem* item = myList->item(0);
qDebug() << item->text() << item->data(MyIntValue).toInt() << item->data(MyStringValue).toString();

You can store as many items as you want with this method.