PDA

View Full Version : Drag/Drop from ListWidget to GraphicsView :-(



drumboy354
13th November 2011, 17:42
Hello! I’m having troubles getting the drag/drop functionality to work between a listwidget and a qgraphicsview. I can not, for the life of me, figure out how to get the qgraphicsview to show the icon when I drop it into the window. I first actually designed the layout with QT Designer. So gui_from_designer is just the python file that was translated from the “.ui” file. Maybe I'm still not understanding how to correctly implement widgets from QT Designer into code?

My code is fairly simple:


from PyQt4.QtGui import *
from PyQt4.QtCore import *
from gui_from_designer import Ui_MainWindow

class mainWindow(QMainWindow, Ui_MainWindow):

def __init__(self, parent = None):
super(mainWindow, self).__init__(parent)
self.setupUi(self)

self.nodeWindow.setAcceptDrops(True)

#populate listwidget with icons
path = os.path.dirname(sys.argv[0])
for image in sorted(os.listdir(os.path.join(path, "images"))):
if image.endswith(".png"):
item = QListWidgetItem(image.split(".")[0].capitalize())
item.setIcon(QIcon(os.path.join(path,
"images/{}".format(image))))
self.listWidget.addItem(item)

def dragEnterEvent(self, event):
event.accept()

def dropEvent(self, event):
event.accept()

def dragMoveEvent(self, event):
event.accept()


app = QApplication(sys.argv)
form = mainWindow()
form.show()
app.exec_()

My qgraphicsview window is called nodeWindow. I figured maybe instead of:


def dragEnterEvent(self, event):
event.accept()

I would change all the event.accept to self.nodeWindow.event.accept(), like:


def dragEnterEvent(self, event):
self.nodeWindow.event.accept()

But no luck :-\

Is this a question where there is so much wrong with my code I should just start over? I’m still fairly new to PyQt and the way drag/drop works with QGraphicsView so any input would be greatly, greatly appreciated! Thanks so much!