Results 1 to 2 of 2

Thread: Problem with subclassed QListWidgetItem by using drag 'n drop

  1. #1
    Join Date
    Oct 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with subclassed QListWidgetItem by using drag 'n drop

    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?

  2. #2
    Join Date
    Aug 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with subclassed QListWidgetItem by using drag 'n drop

    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.

Similar Threads

  1. Drag and Drop problem
    By ScoOteR in forum Qt Programming
    Replies: 16
    Last Post: 19th May 2010, 14:57
  2. Replies: 4
    Last Post: 25th January 2010, 13:52
  3. QListWidgetItem subclass - Crash program in drag/drop
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2009, 09:24
  4. QListWidgetItem in Drag n' drop
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 3rd April 2009, 14:43
  5. Drag n Drop problem
    By ScoOteR in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2007, 10:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.