PyQt:获取应用程序中文件的文件名 [英] PyQt: Getting file name for file dropped in app

查看:776
本文介绍了PyQt:获取应用程序中文件的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个可以接受havin文件的应用程序。所以,我正在寻找一种方法来提取路径。



现在,我已经为应用程序的正确部分启用了拖放功能,它将接受删除的文本,但我不知道如何处理丢失的文件。



我正在使用:

  def PTE_dragEnterEvent(self,e):
如果e.mimeData()。hasFormat('text / plain'):
e.accept ()
else:
e.ignore()

def PTE_dropEvent(self,e):
newText = self.ui.fileListPTE.toPlainText()+' \\\
\\\
'+ e.mimeData()。text()
self.ui.fileListPTE.setPlainText(newText)

这是稍微修改拖放中提供的代码PyQT4 教程。






我不太可能得到@ekhumoro的答案为我工作,但它给了我更多的地方看,我f oud PyQT4:将文件拖放到QListWidget 中,这有助于。 p>

除了ekhumoro提出的建议,我需要实现拖动移动事件。我终于使用了如下:

  def dragEnterEvent(self,event):
如果event.mimeData()。 hasUrls:
event.accept()
else:
event.ignore()

def dragMoveEvent(self,event):
如果event.mimeData ().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore()

def dropEvent(self,event):
如果event.mimeData()。hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()

newText = self.ui.fileListPTE.toPlainText()
在event.mimeData()中的url urls():
newText + ='\\\
'+ str(url.toLocalFile )
self.ui.fileListPTE.setPlainText(newText)
self.emit(QtCore.SIGNAL(dropped))
else:
event.ignore()


解决方案

QMimeData 类具有处理删除的URL的方法

  def dragEnterEvent(self,event):
如果event.mimeData()。hasUrls():
event.accept()
else:
event.ignore()

def dropEvent(self,event):
在event.mimeData()中的url .urls():
path = url.toLocalFile()。toLocal8Bit()。data()
如果os.path.isfile(路径):
打印路径
#其他东西与路径...


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 now, I have drag and drop enabled for the right part of the application, and it will accept text dropped in, but I do not know how to handle having a file dropped in.

I am using:

def PTE_dragEnterEvent(self, e):
    if e.mimeData().hasFormat('text/plain'):
        e.accept()
    else:
        e.ignore() 

def PTE_dropEvent(self, e):
    newText = self.ui.fileListPTE.toPlainText() + '\n\n' + e.mimeData().text()
    self.ui.fileListPTE.setPlainText(newText)

Which is slightly modifying the code provided in the Drag and Drop in PyQT4 tutorial.


I couldn't quite get @ekhumoro answer to work for me, but it gave me more places to look, and I found PyQT4: Drag and drop files into QListWidget which helped.

In addition to the suggestions made by ekhumoro I needed to implement the drag move event. What I finally used looked like:

def dragEnterEvent(self, event):
    if event.mimeData().hasUrls:
        event.accept()
    else:
        event.ignore()

def dragMoveEvent(self, event):
    if event.mimeData().hasUrls:
        event.setDropAction(QtCore.Qt.CopyAction)
        event.accept()
    else:
        event.ignore()

def dropEvent(self, event):
    if event.mimeData().hasUrls:
        event.setDropAction(QtCore.Qt.CopyAction)
        event.accept()

        newText = self.ui.fileListPTE.toPlainText()
        for url in event.mimeData().urls():
            newText += '\n' + str(url.toLocalFile())
        self.ui.fileListPTE.setPlainText(newText)
        self.emit(QtCore.SIGNAL("dropped"))
    else:
        event.ignore()

解决方案

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...

这篇关于PyQt:获取应用程序中文件的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆