PyQt4 - 将文件拖放到 QPushButton [英] PyQt4 - Dragging and dropping files into QPushButton

查看:32
本文介绍了PyQt4 - 将文件拖放到 QPushButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为标题是不言自明的.我正在创建一个小型独立应用程序,该应用程序需要用户将音频文件拖放到按钮上,然后通过使用文件路径等将文件与硬件上的相应按钮相关联...

I think the title is fairly self explanatory. I'm working to create a small standalone app that requires the user to drag and drop audio files onto buttons to in turn associate the file with a corresponding button on a piece of hardware by using the filepath, etc...

我已经学习了大量关于小部件的拖放教程,而我的朋友有关于列表的教程,但是我开始相信它不能用于按钮?我知道您可以将文本拖放到按钮上.我还没有完全跟上 Qt 的速度,所以我可能只是缺少一个明显的错误.

I've followed a ton of drag and drop tutorials for widgets, and my friend has for lists, however I'm beginning to believe that it can't be done for a button? I'm aware that you can drag and drop text onto a button. I am not fully up to speed with Qt yet so there may just be a glaring error that I'm missing.

这是代码,非常感谢!

import sys
from PyQt4 import QtGui, QtCore

class Button(QtGui.QPushButton):
    def __init__(self, parent):
    super(Button, self).__init__(parent)
    self.setAcceptDrops(True)
    self.setDragDropMode(QAbstractItemView.InternalMove)

def dragEnterEvent(self, event):
    if event.mimeData().hasUrls():
        event.acceptProposedAction()
    else:
        super(Button, self).dragEnterEvent(event)

def dragMoveEvent(self, event):
    super(Button, self).dragMoveEvent(event)

def dropEvent(self, event):
    if event.mimeData().hasUrls():
        for url in event.mimeData().urls():
            path = self.addItem(url.path())
            print path
        event.acceptProposedAction()
    else:
        super(Button,self).dropEvent(event)

class MyWindow(QtGui.QWidget):
    def __init__(self):
        super(MyWindow,self).__init__()
        self.setGeometry(100,100,300,400)
        self.setWindowTitle("Filenames")

        self.btn = QtGui.QPushButton()
        self.btn.setGeometry(QtCore.QRect(90, 90, 61, 51))
        self.btn.setText("Change Me!")
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.btn)

        self.setLayout(layout)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

推荐答案

您发布的代码存在三个问题,主要是您甚至没有使用您制作的自定义 Button 类.您只是在窗口中添加了一个常规按钮:

There are three problems with your posted code, the main being that you aren't even using the custom Button class that you made. You are adding just a regular button to your window with:

self.btn = QtGui.QPushButton()

代替:

self.btn = Button(self)

此外,QPushButtons 没有 setDragDropMode() 方法,因此您需要注释掉该行.反正我也不确定它是做什么的.

Also, QPushButtons don't have a setDragDropMode() method, so you'll need to comment that line out. I'm not sure what it does anyway.

此外,QPushButton 没有 addItem() 方法,所以除非您计划实施它,否则我不确定这是什么.我将其替换为仅打印文件路径.

Also, QPushButton doesn't have an addItem() method so I'm not sure what that's about unless you were planning on implementing it. I replaced it below with just printing the file path.

这是您的代码的工作版本,它只打印拖入按钮的任何文件的文件路径:

Here is a working version of your code, that just prints the file path of any file dragged into the button:

import sys
from PyQt4 import QtGui, QtCore

class Button(QtGui.QPushButton):
    def __init__(self, parent):
        super(Button, self).__init__(parent)
        self.setAcceptDrops(True)
        #self.setDragDropMode(QAbstractItemView.InternalMove)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()
        else:
            super(Button, self).dragEnterEvent(event)

    def dragMoveEvent(self, event):
        super(Button, self).dragMoveEvent(event)

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            for url in event.mimeData().urls():
                print str(url.toLocalFile())
            event.acceptProposedAction()
        else:
            super(Button,self).dropEvent(event)

class MyWindow(QtGui.QWidget):
    def __init__(self):
        super(MyWindow,self).__init__()
        self.setGeometry(100,100,300,400)
        self.setWindowTitle("Filenames")

        self.btn = Button(self)
        self.btn.setGeometry(QtCore.QRect(90, 90, 61, 51))
        self.btn.setText("Change Me!")
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.btn)

        self.setLayout(layout)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

这篇关于PyQt4 - 将文件拖放到 QPushButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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