2012-05-08 22 views
2

this questionとよく似ています。私はPyQtアプリケーションからOSXファイルシステムに画像をドラッグ&ドロップできます。PyQt 4.xからOSXファイルシステムへのドラッグアンドドロップ

ただし、次のコードを使用すると、ドロップ位置に何も表示されません。

私は非常に近いようです。 mimeData.setData(mimeType, byteArray)mimeData.setData("text/plain", selectedImagePath)に変更すると、ドロップ先で「Untitled Clipping」ファイルが取得されるため、少なくともドラッグアンドドロップ操作が動作していることを確認できます。

def startDrag(self, event):  

    selectedImagePath = "/sample/specified/file.jpg" 


    ## convert to a bytestream 
    # 
    mimeData = QtCore.QMimeData() 
    image = QtGui.QImage(selectedImagePath) 
    extension = os.path.splitext(selectedImagePath)[1].strip(".") 
    mimeType = "image/jpeg" if extension in ["jpeg", "jpg"] else "image/png" 

    byteArray = QtCore.QByteArray() 
    bufferTime = QtCore.QBuffer(byteArray) 
    bufferTime.open(QtCore.QIODevice.WriteOnly) 
    image.save(bufferTime, extension.upper()) 

    mimeData.setData(mimeType, selectedImagePath) 

    drag = QtGui.QDrag(self) 
    drag.setMimeData(mimeData) 

    result = drag.start(QtCore.Qt.CopyAction) 

    event.accept() 

どこが間違っていますか?

私はまた、ドロップされたメディアの名前を設定する必要があることを知っているので、それについてのガイダンスも高く評価されます。

答えて

4

イメージ・メイメイティを使用せずにバッファを設定することで、このプロセスを簡略化できます。あなたはURLを使用する場合、それはより普遍的なアプローチになります...

カスタムQLabelのための例:

class Label(QtGui.QLabel): 

    ... 

    def mousePressEvent(self, event): 

     event.accept() 

     selectedImagePath = "/Users/justin/Downloads/smile.png" 

     # a pixmap from the label, or could be a custom 
     # one to represent the drag preview 
     pixmap = self.pixmap() 

     # make sure the thumbnail isn't too big during the drag 
     if pixmap.width() > 320 or pixmap.height() > 640: 
       pixmap = pixmap.scaledToWidth(128) 

     mimeData = QtCore.QMimeData() 
     mimeData.setUrls([QtCore.QUrl(selectedImagePath)]) 

     drag = QtGui.QDrag(self) 
     drag.setMimeData(mimeData) 
     drag.setPixmap(pixmap) 
     # center the hotspot image over the mouse click pos 
     drag.setHotSpot(QtCore.QPoint(
      pixmap.width()/2, 
      pixmap.height()/2)) 

     dropAction = drag.exec_(QtCore.Qt.CopyAction, QtCore.Qt.CopyAction) 

今、デスクトップだけでURLを解釈し、命名は自動です。楽しい!

+0

これを試してみます。あなたは天才です。 – AteYourLembas

関連する問題