【问题标题】:PyQt4 - Dragging and dropping files into QPushButtonPyQt4 - 将文件拖放到 QPushButton
【发布时间】:2015-04-21 18:37:40
【问题描述】:

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

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

这是代码,非常感谢!

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_())

【问题讨论】:

  • 嗨亚当,欢迎来到 SO :)!在这样的问题中,如果您发布更少的代码并显示更多确切问题是什么,您将获得更多回复。在这种情况下,也许您可​​以隔离到表明它可能无法完成的 5-10 行?
  • 嗨,砖,干杯!一直在这里,所以我想我不妨注册——终于! :) 我会这样做,但我不确定错误在哪里,这就是为什么这真的让我难过。脚本本身运行没有任何错误,只是按钮不接受事件
  • 好的!好吧,我做了一个编辑,应该让它更容易阅读,然后希望有 pyqt 经验的人能够提供帮助。祝你好运:D
  • 您查看过发布的任何示例吗? doc.qt.io/qt-5/dnd.html#examples
  • 我已经通读并继续这样做,并且仍然在这里搜索,是的。所有示例都只是在按钮或列表上使用文本。我关注了一个很好的帖子,但它是针对 QList 的:(stackoverflow.com/questions/4151637/…)

标签: python qt drag-and-drop pyqt pyqt4


【解决方案1】:

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

self.btn = QtGui.QPushButton()

代替:

self.btn = Button(self)

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

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

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

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_())

【讨论】:

  • 非常感谢!大部分代码只是从我在 cmets 中提到的 QList 拖放线程中解放出来的,正如您所知,我仍然对 Qt 有所了解。所以非常感谢!
  • 感谢这个例子!对于那些想将它与 PyQt5 一起使用的人,它对我有用,我只需将模块“QtGui”重命名为“QtWidgets”
猜你喜欢
  • 2011-05-08
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多