Issue
If I run this code:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setGeometry(200,200,800,400)
self.mainWindowWidget = QWidget()
self.setCentralWidget(self.mainWindowWidget)
self.mainWindowLayout = QGridLayout()
self.mainWindowWidget.setLayout(self.mainWindowLayout)
self.DnD = DragnDrop()
self.DnD.setAcceptDrops(True)
self.DnD.setAlignment(Qt.AlignCenter)
self.DnD.setStyleSheet('''
QLabel {
border: 2px solid rgb(0, 0, 0);
color: rgb(0, 0, 0);
background-color: rgb(192, 192, 192);
}
''')
self.DnDList = QListWidget()
self.mainWindowLayout.addWidget(self.DnD, 1, 0)
self.mainWindowLayout.addWidget(self.DnDList, 2, 0)
class DragnDrop(QLabel):
def __init__(self):
QLabel.__init__(self)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
self.test = event.mimeData().urls()
MainWindow().DnDList.addItem(str(self.test))
if __name__ == "__main__":
myApp = QApplication(sys.argv)
QApplication.setStyle(QStyleFactory.create('Plastique'))
mainWindow = MainWindow()
mainWindow.show()
myApp.exec_()
sys.exit(0)
I will get this Run-time error if i drag and drop files on the QLabel:
RuntimeError: wrapped C/C++ object of type QListWidget has been deleted
Sorry if my code example is weak but I am not very proficient in programming. The goal is to drop a file on that qlabel
and get the file-path of it which is then displayed in a qlistwidget
.
I looked at this and this question but failed to fully understand what the problem really is or how I can solve this.
Edit
I like thomasedv's suggestion but fail to realize it.
I tried to emit a custom signal but i dont succeed in passing it to a function of the MainWindow-Class.
In the Code I edited:
def dropEvent(self, event):
self.test = event.mimeData().urls()
MainWindow().DnDList.addItem(str(self.test))
to:
def dropEvent(self, event):
self.emit(SIGNAL("sig"), str(event.mimeData().urls()))
But I am now helpless how to proceed to add that string to my list.
Solution
I can get it to run easily(with PyQt5, had to import some extra stuff, but i guess that's a difference between PySide and PyQt5), but the drag and drop doesn't work, and i think that's your issue. I think it's far better to emit a custom signal(if you can do that in PySide like in PyQt5) with the string, and then connect that in your mainwindow class to a function, which adds it to the list you made there. Right now you are trying to add the list element to a MainWindow().DnDList which i don't think is the same as the one created in your Mainwindow class.
Edit: Here is the PyQt5 implementation of the signal/slot method.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setGeometry(200,200,800,400)
self.mainWindowWidget = QWidget()
self.setCentralWidget(self.mainWindowWidget)
self.mainWindowLayout = QGridLayout()
self.mainWindowWidget.setLayout(self.mainWindowLayout)
self.DnD = DragnDrop()
self.DnD.linkDropped.connect(self.urlToList)
self.DnD.setAcceptDrops(True)
self.DnD.setAlignment(Qt.AlignCenter)
self.DnD.setStyleSheet('''
QLabel {
border: 2px solid rgb(0, 0, 0);
color: rgb(0, 0, 0);
background-color: rgb(192, 192, 192);
}
''')
self.DnDList = QListWidget()
self.mainWindowLayout.addWidget(self.DnD, 1, 0)
self.mainWindowLayout.addWidget(self.DnDList, 2, 0)
@pyqtSlot(str)
def urlToList(self, url):
self.DnDList.addItem(url)
class DragnDrop(QLabel):
linkDropped = pyqtSignal(str)
def __init__(self):
QLabel.__init__(self)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
self.test = event.mimeData().urls()
self.linkDropped.emit(self.test[0].toString())
if __name__ == "__main__":
myApp = QApplication(sys.argv)
QApplication.setStyle(QStyleFactory.create('Plastique'))
mainWindow = MainWindow()
mainWindow.show()
myApp.exec_()
sys.exit(0)
Few things to note, self.test is actually a list with QUrls. So you need to use the 0 index. If you want to support drag and drop of multiple files, iterate through the items in the list, convert each item to string and emit for each element, like so:
for i in self.test:
self.linkDropped.emit(i.toString())
Each url starts with a file:\\\ prefix since i'm sure that's in the browser, might need to change that a bit if you don't want it with:
i.toString()[8:]
Answered By - Thomasedv
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.