我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用PyQt4.QtCore.QMimeData()。
def startDrag(self, event): # item is of type NodeListItem item = self.currentItem() # nodeData is the data (BaseNode type) that item was created with nodeData = nt.NodeTypes[item.dictKey] #nodeData = mayaNodesModule.MayaNodes[item.dictKey]) i = id(nodeData) self.d[i] = nodeData pickleData = cPickle.dumps(i) data = QtCore.QByteArray.fromRawData(pickleData) mimeData = QtCore.QMimeData() mimeData.setData("application/x-imgname", data) drag = QtGui.QDrag(self) drag.setMimeData(mimeData) # Setting the icon that the mouse cursor displays icon = item.icon() pixmap = icon.pixmap(48, 48) drag.setPixmap(pixmap.scaled(pixmap.height()*.5, pixmap.width()*.5)) # Actually starts the dragging drag.exec_()
def createDragData(self): self.drag = QtGui.QDrag(self) data = QtCore.QMimeData() wave_len = self.selection[1] + 1 - self.selection[0] samples = self.current_data[self.selection[0] * 256 + self.offset:(self.selection[1] + 1) * 256 + self.offset] data.setData('audio/wavesamples', samples) data.setText(self.current_source) path = QtGui.QPainterPath() sampwidth_int = self.current_sampwidth_int / 2 path.moveTo(0, sampwidth_int - audioop.getsample(samples, self.current_sampwidth, 0)) for s in xrange(1, len(samples)/2): path.lineTo(s, sampwidth_int - audioop.getsample(samples, self.current_sampwidth, s)) wave_size = self.main.wavetable_view.width() / 64 pixmap = QtGui.QPixmap(wave_size * wave_len, 48) pixmap.fill(QtCore.Qt.transparent) qp = QtGui.QPainter(pixmap) qp.setRenderHints(qp.Antialiasing) qp.scale((wave_size * wave_len / path.boundingRect().width()), 48. / self.current_sampwidth_int) qp.drawPath(path) qp.end() self.drag.setPixmap(pixmap) self.drag.setMimeData(data) self.drag.exec_()
def mousePressEvent(self, event): #print ('Mouse press...') hotSpot = event.pos() mimeData = QtCore.QMimeData() mimeData.setText(self.text()) mimeData.setData('application/x-hotspot', '%d %d' % (hotSpot.x(), hotSpot.y())) pixmap = QtGui.QPixmap(self.size()) self.render(pixmap) drag = QtGui.QDrag(self) drag.setMimeData(mimeData) drag.setPixmap(pixmap) drag.setHotSpot(hotSpot) dropAction = drag.exec_(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction, QtCore.Qt.CopyAction) if dropAction == QtCore.Qt.MoveAction: self.close() self.update()
def mimeData(self, idxs): mimedata = QtCore.QMimeData() for idx in idxs: if idx.isValid(): txt = self.data(idx, QtCore.Qt.DisplayRole) mimedata.setData('component/name', txt) return mimedata #------------------------------------------------------------------------------------------------ #========================================================================================================================
def mimeData(self, indexes): """ This function is called by QT each time a drag operation is initiated, to serialize the data associated with the dragged item. However, QT doesn't know how to serialize a Shape or a Layer, so it throws an error ... since we handle Drag & Drop internally, we don't need any serialization, so we subclass the function and return nothing (trick to avoid errors). """ mimeData = QtCore.QMimeData() #mimeData.setData("application/x-qabstractitemmodeldatalist", "") mimeData.setData("application/x-qstandarditemmodeldatalist", "") return mimeData
def mousePressEvent(self, e): Mainframe.sigWrapper.sigFocusOnPieces.emit() if e.buttons() != QtCore.Qt.LeftButton: # On right click # if the square is empty, add the selected piece # if the square is non-empty, remove it if Mainframe.model.board.board[self.id] is None: if Mainframe.selectedPiece is not None: Mainframe.model.board.add(model.Piece(Mainframe.selectedPiece.name, Mainframe.selectedPiece.color, Mainframe.selectedPiece.specs), self.id) else: return else: Mainframe.selectedPiece = Mainframe.model.board.board[self.id] Mainframe.model.board.drop(self.id) Mainframe.model.onBoardChanged() Mainframe.sigWrapper.sigModelChanged.emit() return if Mainframe.model.board.board[self.id] is None: return # ctrl-drag copies existing piece modifiers = QtGui.QApplication.keyboardModifiers() Mainframe.currentlyDragged = Mainframe.model.board.board[self.id] if not (modifiers & QtCore.Qt.ControlModifier): Mainframe.model.board.drop(self.id) Mainframe.model.onBoardChanged() Mainframe.sigWrapper.sigModelChanged.emit() mimeData = QtCore.QMimeData() drag = QtGui.QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.pos() - self.rect().topLeft()) dropAction = drag.start(QtCore.Qt.MoveAction) Mainframe.currentlyDragged = None
def mousePressEvent(self, e): if self.piece is None: return if e.buttons() != QtCore.Qt.LeftButton: return Mainframe.currentlyDragged = self.piece mimeData = QtCore.QMimeData() drag = QtGui.QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.pos() - self.rect().topLeft()) dropAction = drag.start(QtCore.Qt.MoveAction) Mainframe.currentlyDragged = None
def __init__(self): QtCore.QMimeData.__init__(self) self.referenceDataObject = None
def hasFormat(self, mime): if mime == 'data/reference' and self.referenceDataObject: return True return QtCore.QMimeData.hasFormat(self, mime)