PyQt: Getting file name for file dropped in app

前端 未结 1 867
北荒
北荒 2020-12-16 16:29

I am trying to set up an application that will accept havin files dropped into it. So, I am looking for a way to extract the path when they are dropped in.

Right

1条回答
  •  执笔经年
    2020-12-16 16:50

    The QMimeData class has methods for dealing with dropped urls:

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.accept()
        else:
            event.ignore()
    
    def dropEvent(self, event):
        for url in event.mimeData().urls():
            path = url.toLocalFile().toLocal8Bit().data()
            if os.path.isfile(path):
                print path
                # do other stuff with path...
    

    0 讨论(0)
提交回复
热议问题