[PyQt] Drop to system

Massimo Di Stefano massimodisasha at gmail.com
Thu Sep 9 20:45:56 BST 2010


Thanks Baz,

that works perfectly,
thanks to David too, who has explained me how to handle the "drag actions" inside a QtLabel and Qtable

i used this code to populate a QtTable with the full path to the files in a directory, each item is draggable in my app,  the mime tipe is automagically recoglized by the file extension :


import sys, os
from PyQt4.QtCore import Qt, QMimeData, QUrl, QDir
from PyQt4.QtGui import QApplication, QDrag, QTableWidget, QTableWidgetItem


class TableWidget(QTableWidget):
    def __init__(self):
        QTableWidget.__init__(self)
        
        kmldir = '/Users/sasha/kml/'
        kmlfiles = self.listkml(kmldir)
        self.setRowCount(len(kmlfiles))
        self.setColumnCount(1)
        self.setDragEnabled(True)
        
        for i in range(len(kmlfiles)):
            item = QTableWidgetItem()
            item.setText(os.path.join(kmldir, kmlfiles[i]))
            item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled)
            self.setItem(i, 0, item)
    
    def listkml(self,kmldir):
        dirList = os.listdir(kmldir)
        listakml = []
        for fname in dirList:
            listakml.append(fname)
        return listakml
    
    def mimeData(self, items):
    
        data = QMimeData()
        urls = []
        for item in items:
            urls.append(QUrl.fromLocalFile(item.text()))
        
        data.setUrls(urls)
        return data


if __name__ == "__main__":

   app = QApplication(sys.argv)
   window = TableWidget()
   window.show()

   sys.exit(app.exec_())



thanks to All the PyQt community for the powerfull help you provide !!!

Happy coding to All,

Massimo.

Il giorno 09/set/2010, alle ore 19.01, Baz Walter ha scritto:

> On 09/09/10 14:29, Massimo Di Stefano wrote:
>> tring to learn how to use the Drag class, i tried to hack the "draggabletext.py" from the pyqt source :
>> 
>> - http://www.geofemengineering.it/draggabletext_.py
>> 
>> i'm now blocked on how to reuse the same code
>> to drag a QTableWidgetItem from a QtTablewidjet,
>> the code i'm starting from is :
>> 
>> - http://www.geofemengineering.it/table_ui.py
>> 
>> - http://www.geofemengineering.it/table.py
>> 
>> i'm tring to learn how to use the class " bool QTableWidget::dropMimeData " have you any hints ?
> 
> i don't know what program you're using to view kml files, but the following works for me if i drop the url onto google-earth:
> 
> 
> import sys, os
> from PyQt4.QtCore import Qt, SIGNAL, QMimeData, QUrl
> from PyQt4.QtGui import (
>    QApplication, QTableWidget, QTableWidgetItem, QDrag,
>    )
> 
> class Window(QTableWidget):
>    def __init__(self):
>        QTableWidget.__init__(self, 1, 1)
>        self.setEditTriggers(QTableWidget.NoEditTriggers)
>        self.connect(self, SIGNAL('itemPressed(QTableWidgetItem*)'),
>                     self.handleItemPressed)
>        kmlfile = os.path.join(os.getcwd(), 'test.kml')
>        self.setItem(0, 0, QTableWidgetItem(kmlfile))
> 
>    def handleItemPressed(self, item):
>        if app.mouseButtons() == Qt.LeftButton:
>            drag = QDrag(self)
>            data = QMimeData()
>            data.setUrls([QUrl.fromLocalFile(item.text())])
>            drag.setMimeData(data)
>            drag.exec_()
> 
> 
> if __name__ == "__main__":
>   app = QApplication(sys.argv)
>   win = Window()
>   win.show()
>   sys.exit(app.exec_())
> 



More information about the PyQt mailing list