我正在使用PyQt5,我想制作一个Drag& Drop系统。
我在这篇文章中得到了代码:PyQT4: Drag and drop files into QListWidget
当我运行时,我收到以下错误:AttributeError:'MainForm'对象没有属性'connect'
代码:
import sys
import os
from PyQt5.Qt import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class TestListView(QListWidget):
def __init__(self, type, parent=None):
super(TestListView, self).__init__(parent)
self.setAcceptDrops(True)
self.setIconSize(QSize(72, 72))
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
links = []
for url in event.mimeData().urls():
links.append(str(url.toLocalFile()))
self.emit(Qt.SIGNAL("dropped"), links)
else:
event.ignore()
class MainForm(QMainWindow):
def __init__(self, parent=None):
super(MainForm, self).__init__(parent)
self.view = TestListView(self)
self.connect(self.view, Qt.SIGNAL("dropped"), self.pictureDropped)
self.setCentralWidget(self.view)
def pictureDropped(self, l):
for url in l:
if os.path.exists(url):
print(url)
icon = QIcon(url)
pixmap = icon.pixmap(72, 72)
icon = QIcon(pixmap)
item = QListWidgetItem(url, self.view)
item.setIcon(icon)
item.setStatusTip(url)
def main():
app = QApplication(sys.argv)
form = MainForm()
form.show()
app.exec_()
if __name__ == '__main__':
main()
答案 0 :(得分:7)
问题是您使用的是旧的连接形式,您必须更新以下内容:
创建信号:
class TestListView(QListWidget):
dropped = pyqtSignal(list)
同样改变:
self.emit(Qt.SIGNAL("dropped"), links)
为:
self.dropped.emit(links)
并改变:
self.connect(self.view, Qt.SIGNAL("dropped"), self.pictureDropped)
为:
self.view.dropped.connect(self.pictureDropped)
有关详细信息,请参阅here
完整代码:
import sys
import os
from PyQt5.Qt import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class TestListView(QListWidget):
dropped = pyqtSignal(list)
def __init__(self, type, parent=None):
super(TestListView, self).__init__(parent)
self.setAcceptDrops(True)
self.setIconSize(QSize(72, 72))
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
links = []
for url in event.mimeData().urls():
links.append(str(url.toLocalFile()))
self.dropped.emit(links)
else:
event.ignore()
class MainForm(QMainWindow):
def __init__(self, parent=None):
super(MainForm, self).__init__(parent)
self.view = TestListView(self)
self.view.dropped.connect(self.pictureDropped)
self.setCentralWidget(self.view)
def pictureDropped(self, l):
for url in l:
if os.path.exists(url):
print(url)
icon = QIcon(url)
pixmap = icon.pixmap(72, 72)
icon = QIcon(pixmap)
item = QListWidgetItem(url, self.view)
item.setIcon(icon)
item.setStatusTip(url)
def main():
app = QApplication(sys.argv)
form = MainForm()
form.show()
app.exec_()
if __name__ == '__main__':
main()