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.