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
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.
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()
form = mainWindow()
form.show()
app.exec_()
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_()
To copy to clipboard, switch view to plain text mode
My qgraphicsview window is called nodeWindow. I figured maybe instead of:
def dragEnterEvent(self, event):
event.accept()
def dragEnterEvent(self, event):
event.accept()
To copy to clipboard, switch view to plain text mode
I would change all the event.accept to self.nodeWindow.event.accept(), like:
def dragEnterEvent(self, event):
self.nodeWindow.event.accept()
def dragEnterEvent(self, event):
self.nodeWindow.event.accept()
To copy to clipboard, switch view to plain text mode
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!
Bookmarks