def mouseMoveEvent(self, event): """ Override the behaviour when the mouse is moved Doing this we avoid that the tab is moved when tearing off the tab """ if self._select_tab_index < -1: pass else: if event.modifiers() == Qt.ControlModifier: self.setCursor(Qt.OpenHandCursor) else: self.setCursor(Qt.ArrowCursor) if not event.buttons() == Qt.LeftButton: return if not self._drag_start_pos.isNull() and ( event.pos() - self._drag_start_pos ).manhattanLength() < QApplication.startDragDistance(): self._drag_initiated = True if (event.buttons() == Qt.LeftButton ) and self._drag_initiated and not self.geometry().contains( event.pos()): finish_move_event = QMouseEvent(QEvent.MouseMove, event.pos(), Qt.NoButton, Qt.NoButton, Qt.NoModifier) super(TearOffTabBar, self).mouseMoveEvent(finish_move_event) drag = QDrag(self) mime_data = QMimeData() mime_data.setData('action', 'application/tab-detach') drag.setMimeData(mime_data) rect = self.tabRect(self.tabAt(event.pos())) pixmap_big = QPixmap.grabWidget( self.parentWidget().currentWidget()).scaled( 640, 480, Qt.KeepAspectRatio) drag.setPixmap(pixmap_big) drag.setDragCursor(QPixmap(), Qt.LinkAction) dragged = drag.exec_(Qt.MoveAction) if dragged == Qt.IgnoreAction: # moved outside of tab widget event.accept() self.tabDetached.emit(self.tabAt(self._drag_start_pos), QCursor.pos()) elif dragged == Qt.MoveAction: # moved inside of tab widget if not self._drag_dropped_pos.isNull(): event.accept() self.tabMoved.emit(self.tabAt(self._drag_start_pos), self.tabAt(self._drag_dropped_pos)) self._drag_dropped_pos = QPoint() else: super(TearOffTabBar, self).mouseMoveEvent(event)
def startDrag(self, supported_actions): """ Function that is called when we start dragging an item This function set ups the mime data and creates a copy of the image of the item that is show in cursor position while dragging action is enabled """ index = self.currentIndex() model = self.model() item = model.itemFromIndex(index) drag = QDrag(self) mime_data = QMimeData() mime_data.setData('item_text', QByteArray(str(item.text()))) mime_data.setData('item_size', QByteArray('%.1f %.1f' % (item.size.width(), item.size.height()))) url = QUrl.fromLocalFile(item.text()) mime_data.setUrls(url) drag.setMimeData(mime_data) pixmap = item.icon().pixmap(50, 50) drag.setDragCursor(pixmap, Qt.CopyAction) drag.start()