0
votes

I have my own class inheriting from QListWidgetItem and another inheriting from QListWidget. I am trying to make Drag and Drop work properly. I want to insert items exactly where indicator is. I can't use standard QListWidget.dropEvent because then it's trying to drop QListWidgetItems... not objects of my class. Here is part of my dropEvent but It's not really good:

def dropEvent(self, event):
    item=event.source().currentItem().clone()
    oldrow=event.source().row(event.source().currentItem())
    o=event.source().takeItem(oldrow)
    del(o)
    cursorpos=self.mapFromGlobal(QtGui.QCursor.pos())
    itembefore=self.itemAt(cursorpos)
    print itembefore
    if itembefore:
        row=self.row(itembefore)
        self.insertItem(row, item)


    else:
        self.addItem(item)

Now it always drops items After the item you drop on... and it's not always where indicator is shown. Some ideas?

2

2 Answers

1
votes

I have found some workaround. A little dirty but works:

def dropEvent(self, event):
    olditem=event.source().currentItem()
    currentrow=event.source().row(olditem)
    item=event.source().takeItem(currentrow)
    QtGui.QListWidget.dropEvent(self,event)
    newItem=self.findItems(olditem.text(),QtCore.Qt.MatchExactly)[0]
    row=self.row(newItem)
    o=self.takeItem(row)
    del(o)
    if event.source()!=self:
        self.insertItem(row,item)
        self.emit(QtCore.SIGNAL("moveTask"),item.itemid,self.date)
    else:
        del(olditem)
        self.insertItem(row,item)  
    event.accept()       
    self.emit(QtCore.SIGNAL("sortTasks"),self)

I am using standard dropEvent only to get row number...

0
votes

I don't know python but in C++ you could use casts to solve your problem. (Pseudo-C++ Code:)

YourListWidget::dropEvent(QDropEvent * event) {
    YourListWidgetItemClass* item = qobject_cast<YourListWidgetItemClass* >(event->source()->currentItem());
}