[PyKDE] drag'n drop: how to notify source of drag?

ralph ralph at strubi.ox.ac.uk
Thu Jan 29 23:25:00 GMT 2004


Hi,
 
I'm trying to implement drag and drop on QTable cells in order to be 
able to move the content from one cell to another, and even to a  
cell of another QTable. Moving within the same table is easy to 
handle, but my problem is: How do I know on source side that 
a drop was successfully done on the target widget if those are  
e.g. two completely independent widget/applications? 
I was running the code listed below twice from two different shells 
and trying to move one cell from one table to the other.  
Also the documentation states that the 'QTable.dropped()' signal is fired, 
but I don't receive it (except if QTable.contentDropEvent() is NOT 
coded - isn't this weird?). Is 'dropped()' supposed to be triggered 
on the source or on the target widget? And only in certain circumstances? 
Is this the right signal to look at? 
Any help would be appreciated.
 
[I'm running QT 3.1.1, PyQt 3.10+sip-4rc2 on Linux, same problem
 with PyQt 3.7+sip-3.7]
 
Regards,
 
Ralph

 
------------------------------------------------ 
from qt import * 
from qttable import QTable
 
class DnDTable(QTable):
    def __init__(self, rows, columns, *args, **kw):
        QTable.__init__(self, *args, **kw)
        self.setDragEnabled(1)
        self.viewport().setAcceptDrops(True)
        self.setNumRows(rows)
        self.setNumCols(columns)
        self.resize(300,250)
        self.connect(self, SIGNAL("dropped(QDropEvent*)"), self.wasDropped)

    def setCell(self, row, col, text):
        self.setText(row, col, text)

    def wasDropped(self, dropEvent):
        print 'dropped!', dropEvent.isAccepted()

    def dragObject(self):
        r, c = self.currentRow(), self.currentColumn()
        if self.item(r,c):
            print 'dragging obj from',r,c
            dragObject = QTextDrag('%s' % self.text(r,c), self)
            return dragObject

    def _getEventCoordinates(self, event):
        r = self.rowAt(event.pos().y())
        c = self.columnAt(event.pos().x())
        return r,c

    def contentsDragMoveEvent(self, dragEvent):
        r,c = self._getEventCoordinates(dragEvent)
        #if str(self.text(r,c))=='' and r>=0 and c>=0 and \
        if not self.item(r,c) and r>=0 and c>=0 and \
               QTextDrag.canDecode(dragEvent):
            dragEvent.accept()
        else:
            dragEvent.ignore()

    def contentsDropEvent(self, dropEvent):
        r,c = self._getEventCoordinates(dropEvent)
        s = QString()
        if (QTextDrag.decode(dropEvent, s)):
            if dropEvent.source() == self:
                print 'dropping obj %s from self at' % str(s),r,c
                # get source coordinates:
                sr, sc = self.currentRow(), self.currentColumn()
                item = self.item(sr, sc)
                self.takeItem(item)
                self.setItem(r, c, item)
            else:
                print 'dropping obj %s from outside source at' % str(s),r,c
                self.setCell(r, c, str(s))
            self.setCurrentCell(r,c)
            dropEvent.accept(True)
 
if __name__ == "__main__":
    import sys
    a = QApplication(sys.argv)
    QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()"))
    w = DnDTable(3,4)
    a.setMainWidget(w)
    w.show()
    w.setReadOnly(1)
    w.setCell(1,1,'AAA')
    w.setCell(2,2,'ZZZ')
    a.exec_loop()
 




More information about the PyQt mailing list