def dragDrop(widget: QWidget, mime: QMimeData, pos: QPoint = QPoint(), action=Qt.CopyAction, buttons=Qt.LeftButton, modifiers=Qt.NoModifier) -> bool: """ Simulate a drag/drop interaction on the `widget`. A `QDragEnterEvent`, `QDragMoveEvent` and `QDropEvent` are created and dispatched to the `widget`. However if any of the `QDragEnterEvent` or `QDragMoveEvent` are not accepted, a `QDragLeaveEvent` is dispatched to 'reset' the widget state before this function returns `False` Parameters ---------- widget: QWidget The target widget. mime: QMimeData The mime data associated with the drag/drop. pos: QPoint Position of the drop action: Qt.DropActions Type of acceptable drop actions buttons: Qt.MouseButtons: Pressed mouse buttons. modifiers: Qt.KeyboardModifiers Pressed keyboard modifiers. Returns ------- state: bool Were the events accepted. See Also -------- QDragEnterEvent, QDropEvent """ if pos.isNull(): pos = widget.rect().center() ev = QDragEnterEvent(pos, action, mime, buttons, modifiers) ev.setAccepted(False) QApplication.sendEvent(widget, ev) ev = QDragMoveEvent(pos, action, mime, buttons, modifiers) ev.setAccepted(False) QApplication.sendEvent(widget, ev) if not ev.isAccepted(): QApplication.sendEvent(widget, QDragLeaveEvent()) return False ev = QDropEvent(QPointF(pos), action, mime, buttons, modifiers) ev.setAccepted(False) QApplication.sendEvent(widget, ev) return ev.isAccepted()
def dragDrop(widget: 'QWidget', mime: QMimeData, pos: QPoint = QPoint(-1, -1), action=Qt.CopyAction, buttons=Qt.LeftButton, modifiers=Qt.NoModifier) -> bool: if pos == QPoint(-1, -1): pos = widget.rect().center() ev = QDragEnterEvent(pos, action, mime, buttons, modifiers) ev.setAccepted(False) QApplication.sendEvent(widget, ev) if not ev.isAccepted(): return False ev = QDropEvent(QPointF(pos), action, mime, buttons, modifiers) ev.setAccepted(False) QApplication.sendEvent(widget, ev) return ev.isAccepted()
def test_drop(self): widget = self.widget with tempfile.TemporaryDirectory() as tmpdir: urlpath = QUrl.fromLocalFile(tmpdir) data = QMimeData() data.setUrls([urlpath]) pos = widget.recent_cb.rect().center() actions = Qt.LinkAction | Qt.CopyAction ev = QDragEnterEvent(pos, actions, data, Qt.LeftButton, Qt.NoModifier) assert QApplication.sendEvent(widget.recent_cb, ev) self.assertTrue(ev.isAccepted()) del ev ev = QDropEvent(pos, actions, data, Qt.LeftButton, Qt.NoModifier, QDropEvent.Drop) assert QApplication.sendEvent(widget.recent_cb, ev) self.assertTrue(ev.isAccepted()) del ev self.assertEqual(widget.currentPath, urlpath.toLocalFile()) self._startandwait(widget) self.widget.commit()
def test_drop(self): widget = self.widget with tempfile.TemporaryDirectory() as tmpdir: urlpath = QUrl.fromLocalFile(tmpdir) data = QMimeData() data.setUrls([urlpath]) pos = widget.recent_cb.rect().center() actions = Qt.LinkAction | Qt.CopyAction ev = QDragEnterEvent(pos, actions, data, Qt.LeftButton, Qt.NoModifier) assert QApplication.sendEvent(widget.recent_cb, ev) self.assertTrue(ev.isAccepted()) del ev ev = QDropEvent(pos, actions, data, Qt.LeftButton, Qt.NoModifier, QDropEvent.Drop) assert QApplication.sendEvent(widget.recent_cb, ev) self.assertTrue(ev.isAccepted()) del ev self.assertEqual(widget.recent_paths[0].abspath, urlpath.toLocalFile()) self._startandwait(widget) self.widget.commit()