def mouseMoveEvent(self, event): if (self._clickPos is None or (event.pos() - self._clickPos).manhattanLength() < QApplication.startDragDistance()): return # _log.debug("Drag move") # render before detachment otherwise it might look ugly pixmap = QPixmap(self.size()) self.render(pixmap) self.setVisible(False) if len(self._dock.parentContainer.flatDockList) == 1: self._dock.parentContainer.setVisible(False) # Build drag object event.accept() drag = QDrag(self) mimeData = QMimeData() encodedData = QByteArray(pickle.dumps(self._dock.uid)) mimeData.setData(MIME_TYPE, encodedData) drag.setMimeData(mimeData) drag.setPixmap(pixmap) action = drag.exec_(Qt.MoveAction) _log.debug("After drag. Action: {}".format(action)) if Qt.IgnoreAction == action: _log.debug("D'n'D canceled") self.setVisible(True) self._dock.parentContainer.setVisible(True)
def mouseMoveEvent(self, event): from qtpy.QtCore import QMimeData, QByteArray from qtpy.QtGui import QPixmap, QDrag if not (event.button() != Qt.LeftButton and isinstance(self.dragLabel, QLabel)): return if (event.pos() - self.dragStartPosition ).manhattanLength() < QApplication.startDragDistance(): return axis_index = self.filters_layout.indexOf(self.dragLabel) // 2 # prepare hotSpot, mimeData and pixmap objects mimeData = QMimeData() mimeData.setText(self.dragLabel.text()) mimeData.setData("application/x-axis-index", QByteArray.number(axis_index)) pixmap = QPixmap(self.dragLabel.size()) self.dragLabel.render(pixmap) # prepare drag object drag = QDrag(self) drag.setMimeData(mimeData) drag.setPixmap(pixmap) drag.setHotSpot(event.pos() - self.dragStartPosition) drag.exec_(Qt.MoveAction | Qt.CopyAction, Qt.CopyAction)
def drag_with_pixmap(list_widget: QListWidget) -> QDrag: """Create a QDrag object with a pixmap of the currently select list item. This method is useful when you have a QListWidget that displays custom widgets for each QListWidgetItem instance in the list (usually by calling ``QListWidget.setItemWidget(item, widget)``). When used in a ``QListWidget.startDrag`` method, this function creates a QDrag object that shows an image of the item being dragged (rather than an empty rectangle). Parameters ---------- list_widget : QListWidget The QListWidget for which to create a QDrag object. Returns ------- QDrag A QDrag instance with a pixmap of the currently selected item. Examples -------- >>> class QListWidget: ... def startDrag(self, supportedActions): ... drag = drag_with_pixmap(self) ... drag.exec_(supportedActions, Qt.MoveAction) """ drag = QDrag(list_widget) drag.setMimeData(list_widget.mimeData(list_widget.selectedItems())) size = list_widget.viewport().visibleRegion().boundingRect().size() pixmap = QPixmap(size) pixmap.fill(Qt.transparent) painter = QPainter(pixmap) for index in list_widget.selectedIndexes(): rect = list_widget.visualRect(index) painter.drawPixmap(rect, list_widget.viewport().grab(rect)) painter.end() drag.setPixmap(pixmap) drag.setHotSpot(list_widget.viewport().mapFromGlobal(QCursor.pos())) return drag