Hello I have followed the mailing list a while and been using PySide since it start working on Windows

I have been struggling with a drag and drop operation using QStandardItemModel and a QListView for a while.

My goal is to use my own mime-type and send data defined in my custom item model to another widget in the application. Whatever I did resulted in the following error in one or another form:

python.exe main.py
Traceback (most recent call last):
  File "main.py", line 23, in mimeData
    self.__mimedata.setData(u'application/my-form',self.__qextra)
RuntimeError: Internal C++ object (PySide.QtCore.QMimeData) already deleted.
Traceback (most recent call last):
  File "main.py", line 23, in mimeData
    self.__mimedata.setData(u'application/my-form',self.__qextra)
RuntimeError: Internal C++ object (PySide.QtCore.QMimeData) already deleted.

I then created the attached application and tested it both PyQt4 and PySide. It works as expected in PyQt4 but gives the error above in PySide.

I guess I have found a bug in the QMimeData model or do I used PySide and QMimeData the wrong way?

I am using PySide 1.0.0.0 beta4 with Qt 4.7.1 and Python 2.6.6 on Windows 7

Thanks,
Bjorn Helge Kjosnes
from PySide import QtCore, QtGui
#from PyQt4 import QtCore, QtGui

class MyItemModel(QtGui.QStandardItemModel):
    def __init__(self,parent=None):
        super(MyItemModel,self).__init__(parent)
        self.appendRow([QtGui.QStandardItem('Item 1'),])
        self.appendRow([QtGui.QStandardItem('Item 2'),])
        self.appendRow([QtGui.QStandardItem('Item 3'),])
        self.appendRow([QtGui.QStandardItem('Item 4'),])
    def mimeTypes(self):
        mtypes = super(MyItemModel,self).mimeTypes()
        mtypes.append(u'application/my-form')
        return mtypes
    def mimeData(self,indexes):
        self.__mimedata = super(MyItemModel,self).mimeData(indexes)
        extra = 'drop=['
        for index in indexes:
            item = self.data(index)
            extra += '"%s"' % item.__str__()
        extra+=']'
        self.__qextra = QtCore.QByteArray(extra)
        self.__mimedata.setData(u'application/my-form',self.__qextra)
        return self.__mimedata

class MyTextEdit(QtGui.QTextEdit):
    def __init__(self,parent=None):
        super(MyTextEdit,self).__init__(parent)
    def dragEnterEvent(self,event):
        event.acceptProposedAction()
    def dragMoveEvent(self,event):
        event.acceptProposedAction()
    def dropEvent(self,event):
        formats = event.mimeData().formats()
        for format in formats:
            fs = format.split(';')
            if len(fs)>1:
                t = "%s\t%s\n" % (fs[0],fs[1])
            else:
                t = "%s\n" % (fs[0])
            if fs[0]=='text/uri-list':
                mdata = event.mimeData().data(fs[0])
                ulist = mdata.split('\n')
                for url in ulist:
                    if url:
                        uri = QtCore.QUrl(unicode(url))
                        t = "%s  %s\n" % (t,uri.toLocalFile())
            if fs[0]=='application/my-form':
                mdata = str(event.mimeData().data(fs[0]))
                t = "%s  %s\n" % (t,mdata)
            self.textCursor().insertText(t)
        

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.textEdit = MyTextEdit()
        self.setCentralWidget(self.textEdit)
        self.dock = QtGui.QDockWidget("Drag items", self)
        self.model = MyItemModel(self.dock)
        self.itemlist = QtGui.QListView()
        self.itemlist.setDragEnabled(True)
        self.itemlist.setModel(self.model)
        self.dock.setWidget(self.itemlist)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.dock)

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())
_______________________________________________
PySide mailing list
[email protected]
http://lists.openbossa.org/listinfo/pyside

Reply via email to