Drop on QMdiArea
-
Hi guys,
I've subclassed a QMdiArea to accept drops. Further I've subclassed a QMdiSubWindow to setAcceptDrops to false. Despite that the subwindows accept the drops (propagate the drops to the QMdiArea) which is not desired. Here's the code (in Python):
@
from PyQt4.QtGui import *
from PyQt4.QtCore import *class MDIArea(QMdiArea):
def __init__(self, parent=None): super(MDIArea, self).__init__(parent) self.setAcceptDrops(True) def add_sub_window(self, text): sub_window = MDISubWindow(text) self.addSubWindow(sub_window) sub_window.show() def dropEvent(self, drop_event): mime_data = drop_event.mimeData() text = unicode(mime_data.text()) self.add_sub_window(text) def dragEnterEvent(self, event): event.acceptProposedAction()
class MDISubWindow(QMdiSubWindow):
def __init__(self, text, parent=None): super(MDISubWindow, self).__init__(parent) self.label = QLabel(text) self.setWidget(self.label) self.setAcceptDrops(False)
class TestLabel(QLabel):
def __init__(self, parent=None): super(TestLabel, self).__init__(parent) self.setText("Jan") def mousePressEvent(self, mouse_event): if mouse_event.button() == Qt.LeftButton: self.dragStartPosition = mouse_event.pos() else: self.mousePressEvent(mouse_event) def mouseMoveEvent(self, mouse_event): if mouse_event.buttons() == Qt.LeftButton: if (mouse_event.pos() - self.dragStartPosition).manhattanLength() < QApplication.startDragDistance(): return drag = QDrag(self) mime_data = QMimeData() mime_data.setText(self.text()) drag.setMimeData(mime_data) drop_action = drag.exec_(Qt.MoveAction) else: self.mousePressEvent(mouse_event)
if name == "main":
import sysapp = QApplication(sys.argv) mdi_area = MDIArea() mdi_area.show() test_label = TestLabel() test_label.show() app.exec_()
@
Somehow I managed to get it done in prior versions but can't recall how ... (and didn't commit to vcs ... stupid me).
Any help is highly appreciated,
Jan