Ok, I only did this yesterday for the 1st time and this is what I gathered
from my research.

Each Q*Blar*Event has a mimeData() method.  It's best practice to test the
event.mimeData() type with :
event.mimeData().hasFormat().  That way you can make sure that the data is
of the right type to drop.

dragEventEnter() is automatically called when the mouseEnters the widget
this is called once.

dragMoveEvent() is called as the mouse is moved around your widget.
Implement here anything you need like locked folders, change the pointer
icon etc, etc.

dropEvent() is where you access the mimeData ojb from the event.  Note.  By
default this is not filled with any data.  I think you will need to do this.

startDrag(self, event), this is what is automaticaly called when you set
self.setDragDropMode(QtGui.QAbstractItemView.DragOnly)
This is where you fill the mimeData obj.


This is the 2 class's I wrote yesterday that enable me to drop folder's from
a directory tree into a favorutes ListWidget...,
Again I only did this yesterday so there could be a easier way.  Possibly by
re-implementing mouseEvent()

-Dave


PS.  As you're widget is only dropping to self.  You could test the
mimeData().source() and directly get the selected widget data to drop...


---------------------------------------------------------------------------------------------------------------------------------


class FolderBookMark_ListWidget(QtGui.QListWidget):
    def __init__(self, parent = None):
        super(FolderBookMark_ListWidget,self).__init__(parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        if event.mimeData().hasFormat("application/x-filePath"):
            event.accept()
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasFormat("application/x-filePath"):
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasFormat("application/x-filePath"):
            event.setDropAction(QtCore.Qt.CopyAction)
            data = event.mimeData().data("application/x-filePath")
            stream = QtCore.QDataStream(data, QtCore.QIODevice.ReadOnly)
            filePath = QtCore.QString()
            folderName = QtCore.QString()
            icon = QtGui.QIcon()
            stream >> filePath >> folderName >> icon
            item = BookmarkWidget(icon,folderName)
            item.BookMark = filePath
            self.addItem(item)
            event.accept()
        else:
            event.ignore()


class FolderTreeView(QtGui.QTreeView):
    def __init__(self, parent = None):
        super(FolderTreeView, self).__init__(parent)
        global gFolderDirModel
        global qQDirModelRootPath

gFolderDirModel.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)
        self.setModel(gFolderDirModel)
        self.setRootIndex(gFolderDirModel.index(qQDirModelRootPath))

        self.setColumnHidden(1,True)
        self.setColumnHidden(2,True)
        self.setColumnHidden(3,True)
        self.setDragDropMode(QtGui.QAbstractItemView.DragOnly)

    def GetTreeRoot(self, path):
        filePath = QtCore.QFileInfo(path)
        if filePath.exists():
            Root = None
            if filePath.absoluteFilePath().startsWith("//"):
                path = str(filePath.absoluteFilePath().toUtf8()[2:])
                Root =  "//" + path.partition("/")[0]
            else:
                Root = str(filePath.absoluteFilePath().toUtf8()[:3])
            if Root:
                qQDirModelRootPath = Root
                return Root

    def FocusOnDir(self, Path):
        self.setCurrentIndex(gFolderDirModel.index(Path))
        self.setExpanded(gFolderDirModel.index(Path),True)

    def Goto(self, Path):
        Root = self.GetTreeRoot(Path)
        if Root:
            self.setRootIndex(gFolderDirModel.index(qQDirModelRootPath))
            self.FocusOnDir(Path)

    def startDrag(self, dropAction):
        ModelIndex = self.currentIndex()
        filePath = gFolderDirModel.filePath(ModelIndex)
        folderName = gFolderDirModel.fileName(ModelIndex)
        icon = gFolderDirModel.fileIcon(ModelIndex)

        data = QtCore.QByteArray()
        stream = QtCore.QDataStream(data,QtCore.QIODevice.WriteOnly)
        stream << filePath << folderName << icon
        mimeData = QtCore.QMimeData()
        mimeData.setData("application/x-filePath", data)
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        if drag.start(QtCore.Qt.CopyAction) == QtCore.Qt.CopyAction:
            print "Here"



On Tue, Oct 20, 2009 at 11:26 PM, Taylor Carrasco <crackerbu...@gmail.com>wrote:

> Just trying to do some simple drag drop calls with a QTreeWidget and
> getting nowheres....
>
> What am I missing in dropEvent to get it to actually copy the tree data
> into the new object?
> Currently the dragged object disappears after being dragged onto a new top
> level item, but doesn't show up in the tree.
>
>
>
> class DragDropListWidget(QTreeWidget):
>     def __init__(self, type, parent=None):
>         super(DragDropListWidget, self).__init__(parent)
>
>         #self.dropModes = self.supportedDropActions()
>         self.setAcceptDrops(True)
>         self.setDragDropMode(QAbstractItemView.InternalMove)
>         self.showDropIndicator()
>
>     def dragEnterEvent(self, event):
>         event.accept()
>
>     def dropEvent(self, event):
>         event.accept()
> >
>

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---

Reply via email to