def mouseMoveEvent(self, event): """Start dragging action if needed.""" if not event.buttons() & Qt.LeftButton: return if not self.drag_start_pos: return if not self.drag_indexes: return if (event.pos() - self.drag_start_pos ).manhattanLength() < QApplication.startDragDistance(): return drag = QDrag(self) mimeData = QMimeData() urls = list() for index in self.drag_indexes: file_path = index.data(Qt.UserRole) urls.append(QUrl.fromLocalFile(file_path)) mimeData.setUrls(urls) drag.setMimeData(mimeData) icon = self.drag_indexes[0].data(Qt.DecorationRole) if icon: pixmap = icon.pixmap(32, 32) drag.setPixmap(pixmap) drag.setHotSpot(pixmap.rect().center()) drag.exec_()
def mousePressEvent(self, event): if event.button() == Qt.LeftButton: drag = QDrag(self) mime_data = QMimeData() data_text = self.get_drag_data() data = QByteArray(bytes(data_text, 'utf-8')) mime_data.setData('text/plain', data) drag.setMimeData(mime_data) drop_action = drag.exec_() return
def startDrag(self, event): item = self.itemAt(event.scenePos(), self.page_view_transform) if item is None: event.ignore() return self.item_being_dragged = item.parent self.remove_component_controller(item.parent) hotspot = event.scenePos() - item.scenePos() hot_spot_point = hotspot.toPoint() hot_spot_point = self.page_view_transform.map(hot_spot_point) item_data = QByteArray() data_stream = QDataStream(item_data, QIODevice.WriteOnly) data_stream << hot_spot_point # pixmap << location mime_data = QMimeData() mime_data.setData(config_controller.component_mime, item_data) drag = QDrag(self) drag.setMimeData(mime_data) horizontal_scaling = self.page_view_transform.m11() logger.info(f"sceneBoundingRect {item.sceneBoundingRect().width()}") logger.info( f"sceneBoundingRect {item.sceneBoundingRect().width()*horizontal_scaling}" ) drag.setPixmap( item.parent.pixmap.scaledToWidth(item.sceneBoundingRect().width() * horizontal_scaling)) drag.setHotSpot(hot_spot_point) result = drag.start(QtCore.Qt.MoveAction) if result: # == QtCore.Qt.MoveAction: pass
def startDrag(self, event: QEvent) -> None: item = self.currentItem() if item is None: event.ignore() return self.item_being_dragged = self.takeItem(self.row(item)).parent hot_spot_point = event.pos() item_data = QByteArray() data_stream = QDataStream(item_data, QIODevice.WriteOnly) data_stream << hot_spot_point # pixmap << location mime_data = QMimeData() mime_data.setData(config_controller.component_mime, item_data) drag = QDrag(self) pixmap = item.parent.pixmap.scaledToWidth(64) drag.setHotSpot(QPoint(pixmap.width() / 2, pixmap.height() / 2)) drag.setPixmap(pixmap) drag.setMimeData(mime_data) result = drag.start(QtCore.Qt.MoveAction) if result: # == QtCore.Qt.MoveAction: pass
def startDrag(self, dropActions): item = self.currentItem() # icon = item.icon() data = QByteArray() stream = QDataStream(data, QIODevice.WriteOnly) stream.writeQString(item.text()) stream.writeInt16(item.data(1)) # stream << icon mimeData = QMimeData() mimeData.setData("application/x-lista-item", data) drag = QDrag(self) drag.setMimeData(mimeData) if drag.exec_(Qt.MoveAction) == Qt.MoveAction: self.takeItem(self.row(item))
def mouseMoveEvent(self, event): """Start dragging action if needed""" if not event.buttons() & Qt.LeftButton: return if not self.drag_start_pos: return if (event.pos() - self.drag_start_pos ).manhattanLength() < QApplication.startDragDistance(): return drag = QDrag(self) mime_data = QMimeData() drag.setMimeData(mime_data) pixmap = self.grab() drag.setPixmap(pixmap) drag.setHotSpot(pixmap.rect().center()) drag.exec_()
def fix_pyside_exec(namespace): if namespace.get("__name__") == "AnyQt.QtWidgets": from PySide2.QtWidgets import QApplication, QDialog, QMenu if "exec" not in QApplication.__dict__: QApplication.exec = lambda self: QApplication.exec_() if not hasattr(QDialog, "exec"): QDialog.exec = lambda self: QDialog.exec_(self) if not hasattr(QMenu, "exec"): QMenu.exec = lambda self: QMenu.exec_(self) if namespace.get("__name__") == "AnyQt.QtGui": from PySide2.QtGui import QGuiApplication, QDrag if "exec" not in QGuiApplication.__dict__: QGuiApplication.exec = lambda self: QGuiApplication.exec_() if not hasattr(QDrag, "exec"): QDrag.exec = (lambda self, *args, **kwargs: QDrag.exec_( self, *args, **kwargs)) elif namespace.get("__name__") == "AnyQt.QtCore": from PySide2.QtCore import QCoreApplication, QEventLoop, QThread if not hasattr(QCoreApplication, "exec"): QCoreApplication.exec = lambda self: QCoreApplication.exec_() if not hasattr(QEventLoop, "exec"): QEventLoop.exec = (lambda self, *args, **kwargs: QEventLoop.exec_( self, *args, **kwargs)) if not hasattr(QThread, "exec"): QThread.exec = lambda self: QThread.exec_(self) elif namespace.get("__name__") == "AnyQt.QtPrintSupport": from PySide2.QtPrintSupport import QPageSetupDialog, QPrintDialog if "exec" not in QPageSetupDialog.__dict__: QPageSetupDialog.exec = lambda self: QPageSetupDialog.exec_(self) if "exec" not in QPrintDialog.__dict__: QPrintDialog.exec = lambda self: QPrintDialog.exec_(self)
def mousePressEvent(self, event): super(SnapWindow, self).mousePressEvent(event) if event.button() == QtCore.Qt.MouseButton.LeftButton: self.scale_ratio() self.QDrag = QDrag(self) self.QMimeData = QMimeData() self.imgdata = QByteArray() imgbuffer = QBuffer(self.imgdata) imgbuffer.open(QIODevice.WriteOnly) self.original_snap.save(imgbuffer, "PNG") self.QMimeData.setImageData(self.imgdata) self.QMimeData.setData("image/png", self.imgdata) self.QDrag.setMimeData(self.QMimeData) self.QDrag.setPixmap(self.dragicon) dropaction = self.QDrag.exec_(Qt.CopyAction)
def startDrag(self): data = QByteArray() stream = QDataStream(data, QIODevice.WriteOnly) stream.writeQString(self._player_name) stream.writeInt16(self._player_id) mimedata = QMimeData() mimedata.setData("application/x-lista-item", data) drag = QDrag(self) drag.setMimeData(mimedata) if drag.exec_(Qt.MoveAction) == Qt.MoveAction: self._player_name = "" self._player_id = 0 for i in range(self.sorok_szama): self.parent.eredmenyek[self._csoport_number][ self._csoport_sor][i]._set_p1_id(0) self.parent.eredmenyek[self._csoport_number][i][ self._csoport_sor]._set_p2_id(0) self.update()
def mouseMoveEvent(self, event): """event call when mouse movement (press and move) detected This is the core of the drag and drop for the element. The QDrag object will create a the pixel image of the widget HopSpot is set so that it remains where the grab point is """ if event.buttons() == Qt.LeftButton: drag = QDrag(self) widget_image = self.grab() mimedata = QMimeData() drag.setMimeData(mimedata) drag.setPixmap(widget_image) drag.setHotSpot(event.pos() - self.rect().topLeft()) drag.exec_() super().mouseMoveEvent(event)
def _handle_mouse_press_event_for_dispos(self, ev: QMouseEvent): if ev.button() != QtCore.Qt.LeftButton: return if self.spawns: self.spawn_selected.emit(self) mime_data = QMimeData() mime_data.setData("application/fe14-spawn", b"") drag = QDrag(self) drag.setMimeData(mime_data) drag.setHotSpot(ev.pos()) drag.exec_(QtCore.Qt.MoveAction)
def mouseMoveEvent(self, event): """Start dragging action if needed""" super().mouseMoveEvent(event) if not event.buttons() & Qt.LeftButton: return if not self.drag_start_pos: return if (event.pos() - self.drag_start_pos ).manhattanLength() < QApplication.startDragDistance(): return drag = QDrag(self) drag.setPixmap(self.pixmap) drag.setMimeData(self.mime_data) drag.setHotSpot(self.pixmap.rect().center()) self.drag_start_pos = None self.pixmap = None self.mime_data = None self.drag_about_to_start.emit() drag.exec_()
def mousePressEvent(self, event): # call to super needed for selections super().mousePressEvent(event) if event.button() == Qt.LeftButton: drag = QDrag(self) mimeData = QMimeData() item = self.itemAt(event.pos()) if item is None: return mimeData.setText(item.data(Qt.UserRole)[0]) # should be path to file drag.setMimeData(mimeData) # set dragging pixmap drag.setPixmap(item.icon().pixmap(QSize(160, 120))) # roughly center the cursor relative to pixmap drag.setHotSpot(QPoint(60, 60)) dropAction = drag.exec_()
def startDrag(self, supportedActions): # We reimplement this to visualize a tooltip instead of entire rows while dragging. rows = self.selectionModel().selectedRows() data = self.model().mimeData(rows) label = QLabel( str(len(rows)) + " items" if len(rows) > 1 else "1 item") # Use QPalette.ColorRole? label.setStyleSheet( "font-weight: bold; color : white; background-color : black; border: 1px solid grey" ) pixmap = label.grab() pixmap.rect().adjust(10, 10, 0, 0) drag = QDrag(self) drag.setPixmap(pixmap) # drag.setDragCursor(pixmap, Qt.CopyAction) drag.setMimeData(data) drag.setHotSpot(pixmap.rect().center()) drag.exec_(Qt.CopyAction)
def mouseMoveEvent(self, e): if e.buttons() != Qt.RightButton: return mimeData = QMimeData drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.pos() - self.rect().topLeft()) dropAction = drag.start(Qt.MoveAction)
def mousePressEvent(self, event): label = self.childAt(event.pos()) if not label: return hot_spot = event.pos() - label.pos() mime_data = QMimeData() mime_data.setText(label.text()) mime_data.setData("application/x-hotspot", str(hot_spot.x())) pixmap = QPixmap(label.size()) label.render(pixmap) drag = QDrag(self) drag.setMimeData(mime_data) drag.setPixmap(pixmap) drag.setHotSpot(hot_spot) dropAction = drag.exec_(Qt.CopyAction | Qt.MoveAction, Qt.CopyAction) if dropAction == Qt.MoveAction: label.close()
def mouseMoveEvent(self, event): if not (event.buttons() & Qt.LeftButton): return super(ObjectIcon, self).mouseMoveEvent(event) drag = QDrag(self) mime_data = QMimeData() object_bytes = bytearray() if isinstance(self.object, LevelObject): object_bytes.append(0) else: object_bytes.append(1) object_bytes.extend(self.object.to_bytes()) mime_data.setData("application/level-object", object_bytes) drag.setMimeData(mime_data) if drag.exec_() == Qt.MoveAction: self.object_placed.emit()
def mouseMoveEvent(self, e): # noqa: N802 - Part of QT signature. if e.buttons() != Qt.LeftButton: return mimedata = QMimeData() drag = QDrag(self) drag.setMimeData(mimedata) drag.exec_(Qt.MoveAction)
def mouseMoveEvent(self, e): if e.buttons() != Qt.LeftButton: return mimeData = QMimeData() self.__cursor_x = e.pos().x() self.__cursor_y = e.pos().y() drag = QDrag(self) drag.setMimeData(mimeData) self.hide() drag.setPixmap( self.grab(QRect(2, 2, self.width() - 3.5, self.height() - 3.5))) drag.setHotSpot(e.pos()) dropAction = drag.exec_(Qt.MoveAction) self.show()
def startDrag(self, supportedActions): dragged = self.selectedItems()[0] mimeData = QMimeData() mimeData.setText(dragged.text(0)) drag = QDrag(self) drag.setMimeData(mimeData) drag.exec_(supportedActions=supportedActions, defaultAction=Qt.MoveAction)
def mousePressEvent(self, event): hotSpot = event.pos() mimeData = QMimeData() mimeData.setText(self.text()) mimeData.setData('application/x-hotspot', '%d %d' % (hotSpot.x(), hotSpot.y())) pixmap = QPixmap(self.size()) self.render(pixmap) drag = QDrag(self) drag.setMimeData(mimeData) drag.setPixmap(pixmap) drag.setHotSpot(hotSpot) dropAction = drag.exec_(Qt.CopyAction | Qt.MoveAction, Qt.CopyAction) if dropAction == Qt.MoveAction: self.close() self.update()
def mouseMoveEvent(self, e): if e.buttons() != Qt.RightButton: # 联系Qt.Horizontal, 6.08 return # if-return, 相当于if-else # 鼠标移动时, 应产生mime数据(拖放中传输的数据) # 用于QWidget的拖动释放时, 传输mime数据 mimeData = QMimeData() drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.pos() - self.rect().topLeft()) # 光标位置 dropAction = drag.exec_(Qt.MoveAction)
def mouseMoveEvent(self, event): """start Drag and prepare for Drop. :type event: QMoveEvent マウスを動かした嶺がQApplication.startDragDistance()を超えると、Drag開始されたと認識し、 そのための準備を行う。QMimeDataを使って、データをやりとりする。 """ if not (event.buttons() & Qt.LeftButton): return if (event.pos() - self.dragStartPosition).manhattanLength() \ < QApplication.startDragDistance(): return if self.isDragging: indexes = self.selectedIndexes() urls = self.url_list(indexes) mimeData = QMimeData() # mimeData.setData(self.mime_URLS, convert_to_bytearray(urls)) mimeData.setUrls(urls) file_icon = self.style().standardIcon(QStyle.SP_FileIcon) pixmap = file_icon.pixmap(32, 32) drag = QDrag(self) drag.setMimeData(mimeData) drag.setPixmap(pixmap) drag.setHotSpot(QPoint(0, 0)) dropAction = drag.exec_(Qt.CopyAction | Qt.MoveAction, Qt.CopyAction) if dropAction == Qt.MoveAction: pass else: self.rubberBand.setGeometry( QRect(self.dragStartPosition, event.pos()).normalized()) super(PlaylistView, self).mouseMoveEvent(event)
def mouseMoveEvent(self, event: QMouseEvent): if not event.buttons() and Qt.LeftButton: return # Test if item is drag enabled clicked_item = self.itemAt(self.__dragStartPosition) if clicked_item and not bool(clicked_item.flags() & Qt.ItemIsDragEnabled): return if (event.pos() - self.__dragStartPosition ).manhattanLength() < QApplication.startDragDistance(): return drag: QDrag = QDrag(self) mimeData: QMimeData = QMimeData() mimeData.setText('operation') drag.setMimeData(mimeData) drag.exec_()
def mousePressEvent(self, event): hotSpot = event.pos() mimeData = QMimeData() mimeData.setText(self.text()) mimeData.setData('application/x-hotspot', b'%d %d' % (hotSpot.x(), hotSpot.y())) pixmap = QPixmap(self.size()) self.render(pixmap) drag = QDrag(self) drag.setMimeData(mimeData) drag.setPixmap(pixmap) drag.setHotSpot(hotSpot) dropAction = drag.exec_(Qt.CopyAction | Qt.MoveAction, Qt.CopyAction) if dropAction == Qt.MoveAction: self.close() self.update()
def mouseMoveEvent(self, event): if event.buttons() != Qt.LeftButton: return if (event.pos() - self._dragStartPosition ).manhattanLength() < QApplication.startDragDistance(): return pixmap = QPixmap(23, 23) pixmap.fill(self._color_srgb) drag = QDrag(self) mime_data = QMimeData() text = encode_index(self.parentWidget().index) mime_data.setText(text) drag.setMimeData(mime_data) drag.setPixmap(pixmap) drop_action = drag.start(Qt.CopyAction) # | Qt.MoveAction)
def mouseMoveEvent(self, event: QGraphicsSceneMouseEvent): if (QLineF(event.screenPos(), event.buttonDownScreenPos( Qt.LeftButton)).length() < QApplication.startDragDistance()): return self.parentItem().edgeDragStart() package = QMimeData() package.setData(self.DragMimeType, bytes()) drag = QDrag(event.widget()) drag.setMimeData(package) drag.exec_() self.parentItem().edgeDragStop()
def mouseMoveEvent(self, event): # we should have only a single item selected item = self.selectedItems() if len(item) != 1: return item = item[0] # item should not have a parent (should not be a value) if item.parent() is None: drag = QDrag(self) mime = QtCore.QMimeData() text = item.text(0) if text[0]=='.': text = "s['{}']{}".format(self.nodename, text) mime.setText(text) drag.setMimeData(mime) drag.start(QtCore.Qt.MoveAction)
def itemCliked(self, item): drag = QDrag(self) mimeData = QMimeData() self.selected_project = item.text() mimeData.setText(item.text()) drag.setMimeData(mimeData)
class SnapWindow(QMainWindow): def __init__(self, app, flags): self.flag_snapped = False # Picture snapped or loaded at start. self.flag_frame = True # Window frame toggling. self.app = app self.winsize = None # None-Conditional toggle on resize. # Also the size the window should be. QMainWindow.__init__(self) self.dragicon = QIcon.fromTheme("folder-new").pixmap(QSize(24,24)) self.imgdata = None self.setWindowTitle("Snap") self.cliplabel = QtWidgets.QLabel(self) self.cliplabel.show() self.cliplabel.setScaledContents(True) self.clip_pix = self.cliplabel.pixmap() self.clipboard = app.clipboard() self.clipboard.dataChanged.connect(self.clipboardChanged) self.clipboard.clear(mode=self.clipboard.Clipboard) p = self.palette() p.setColor(self.backgroundRole(), QtCore.Qt.white) self.setPalette(p) self.hide() def load_from_image(self): self.flag_snapped = True im = QtGui.QImage() im.load(shared['inputfp']) pm = QtGui.QPixmap().fromImage(im) self.original_snap = pm.copy() self.original_size = pm.width(), pm.height() mpos = shared['curpos'] self.setGeometry(mpos.x(),mpos.y(), pm.width(), pm.height()) self.cliplabel.resize(pm.width(),pm.height()) self.cliplabel.setPixmap(pm) self.show() def clipboardChanged(self): if self.flag_snapped == False: self.clipboard = self.app.clipboard() pm = self.clipboard.pixmap() self.original_snap = pm.copy() self.original_size = pm.width(), pm.height() if pm.isNull(): pass else: self.flag_snapped = True mpos = shared['curpos'] self.setGeometry(mpos.x()-pm.width(),mpos.y()-pm.height(), pm.width(), pm.height()) self.cliplabel.resize(pm.width(),pm.height()) self.cliplabel.setPixmap(pm) self.show() def resizeEvent(self, event): super(SnapWindow, self).resizeEvent(event) if self.winsize: self.cliplabel.resize(self.savesize.width(),self.savesize.height()) self.resize(self.savesize.width(),self.savesize.height()) self.winsize = None else: self.cliplabel.resize(self.width(),self.height()) def scale_ratio(self): winsize = QSize(self.width(),self.height()) new_size = QSize(self.original_size[0],self.original_size[1]) new_size.scale(winsize, QtCore.Qt.KeepAspectRatio) self.cliplabel.resize(new_size) self.resize(new_size) def reset_size(self): origsize = QSize(self.original_size[0],self.original_size[1]) self.cliplabel.resize(origsize) self.cliplabel.setPixmap(self.original_snap) self.resize(origsize) def mousePressEvent(self, event): super(SnapWindow, self).mousePressEvent(event) if event.button() == QtCore.Qt.MouseButton.LeftButton: self.scale_ratio() self.QDrag = QDrag(self) self.QMimeData = QMimeData() self.imgdata = QByteArray() imgbuffer = QBuffer(self.imgdata) imgbuffer.open(QIODevice.WriteOnly) self.original_snap.save(imgbuffer, "PNG") self.QMimeData.setImageData(self.imgdata) self.QMimeData.setData("image/png", self.imgdata) self.QDrag.setMimeData(self.QMimeData) self.QDrag.setPixmap(self.dragicon) dropaction = self.QDrag.exec_(Qt.CopyAction) def contextMenuEvent(self, event): menu = QtWidgets.QMenu(self) action_rename = menu.addAction("Rename") action_clipboard = menu.addAction("Copy to Clipboard (Again)") action_save = menu.addAction("Save Original") action_reset = menu.addAction("Reset to Original") action_frame = menu.addAction("Toggle Frame") action_close = menu.addAction("Close") action = menu.exec_(self.mapToGlobal(event.pos())) if action == action_save: self.save_copy() elif action == action_clipboard: self.clipboard.setPixmap(self.original_snap) elif action == action_reset: self.reset_size() elif action == action_frame: if self.flag_frame == True: self.flag_frame = False self.setWindowFlag(QtCore.Qt.FramelessWindowHint, True) self.hide() self.show() self.winsize = self.size() else: self.flag_frame = True self.setWindowFlag(QtCore.Qt.FramelessWindowHint, False) self.hide() self.show() self.winsize = self.size() self.reset_size() elif action == action_close: self.close() elif action == action_rename: name, tmp = QInputDialog.getText(self, "", "Name this window:") self.setWindowTitle(name) def save_copy(self): fd = QtWidgets.QFileDialog() fd.setDirectory(QtCore.QDir('~/')) savefn = fd.getSaveFileName(self, 'Save File')[0] pixmap = self.original_snap barray = QtCore.QByteArray() qbuffer = QtCore.QBuffer(barray) qbuffer.open(QtCore.QIODevice.WriteOnly) pixmap.save(qbuffer, "PNG") bytesio = BytesIO(barray.data()) bytesio.seek(0) with open(savefn, 'wb') as savefile: savefile.write(bytesio.read())