Hey guys,
I’m exploring a qt ui for a Maya tool that I’d like the ability to drag and drop icons (qlabels with pixmaps). I’ve looked over the examples in the pyqt folder. Below I listed the snippet of code I’m currently using (taken mostly from the examples), the label is draggable but I can get the “invalid circle symbol” when it follows the cursor. The .ui file I built in Designer has a groupBox with a qlabel on top that will act as a background image for the user. Currently when you release the button while dragging it just puts it back at the creation position. I assume the issue has something to do with stacking widgets? But when I remove the background image qlabel, and just try it on a plain groupbox - the issue is the same.
Thanks for the help!
Jason
class DragLabel( QtGui.QLabel ):
def __init__( self, widget, item, pixmap, listWidget, parent ):
super( DragLabel, self).__init__( parent )
self.parent = parent
self.setPixmap( QtGui.QPixmap( pixmap ) )
self.itemName = item.text()
self.item = item
self.listWidget = listWidget
self.widget = widget
self.setAcceptDrops(True)
#self.setContextMenuPolicy( QtCore.Qt.CustomContextMenu )
#self.customContextMenuRequested.connect( self.popup )
def mousePressEvent( self, event ):
"""
@synopsis: Function for mouse Press event
@para:
event - qt mouse event
"""
# If Right mouse button, display popup menu
if event.button() == QtCore.Qt.RightButton:
self.popup( event.pos() )
else:
pixmap = self.pixmap()
itemData = QtCore.QByteArray()
dataStream = QtCore.QDataStream( itemData, QtCore.QIODevice.WriteOnly )
dataStream << pixmap << QtCore.QPoint( event.pos() - self.rect().topLeft() )
mimeData = QtCore.QMimeData()
mimeData.setData( 'application/x-characterBtn', itemData )
drag = QtGui.QDrag( self )
drag.setMimeData( mimeData )
drag.setPixmap( pixmap )
drag.setHotSpot( event.pos() - self.rect().topLeft() )
self.hide()
if drag.exec_( QtCore.Qt.MoveAction | QtCore.Qt.CopyAction, QtCore.Qt.CopyAction ) == QtCore.Qt.MoveAction:
self.close()
else:
self.show()
class DragWidget( QtGui.QWidget ):
def __init__( self, itemName, pixmap, listWidget, parent=None, x=25, y=25 ):
super(DragWidget, self ).__init__( parent )
self.label = DragLabel( self, itemName, pixmap, listWidget, parent )
self.label.move( x, y )
self.label.show()
self.parent = parent
self.listWidget = listWidget
def dragEnterEvent( self, event ):
if event.mimeData().hasFormat( 'application/x-characterBtn' ):
if event.source() == self:
event.setDropAction( QtCore.Qt.MoveAction )
event.accept()
self.mouseReleaseEvent = self.dropEvent
else:
event.acceptProposedAction()
else:
event.ignore()
def dropEvent( self, event ):
if event.mimeData().hasFormat( 'application/x-characterBtn' ):
itemData = event.mime.data( 'application/x-characterBtn' )
dataStream = QtCore.QDataStream( itemData, QtCore.QIODevice.ReadOnly )
pixmap = QtGui.QPixmap()
offset = QtCore.QPoint()
dataStream >> pixmap >> offset
newLabel = DragLabel( pixmap, self.parent )
newLabel.move( event.pos() - offset )
newLabel.show()
if event.source() == self:
event.setDropAction( QtCore.Qt.MoveAction )
event.accept()
else:
event.acceptProposedAction()
else:
event.ignore()