Issue
hi got this bit of code in PyQt5 (PyQt: v 5.15.7) , basically I am trying to track widget that are dragged outside QMainwindow :
#!/usr/bin/env python3
from PyQt5.QtWidgets import (QApplication, QWidget, QMainWindow,
QVBoxLayout, QPushButton, QGridLayout
)
from PyQt5.QtCore import Qt, QMimeData, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QDrag, QPixmap, QCursor
class DragWidget(QWidget):
def __init__(self, *args, name , **kwargs):
super().__init__(*args, **kwargs)
def mouseMoveEvent(self, e):
if e.buttons() == Qt.LeftButton:
drag = QDrag(self)
mime = QMimeData()
drag.setMimeData(mime)
pixmap = QPixmap(self.size())
self.render(pixmap)
drag.setPixmap(pixmap)
drag.exec_(Qt.MoveAction)
class MainWindow(QMainWindow):
whereDropped = pyqtSignal(int,int)
dragleaves = pyqtSignal()
def __init__(self):
super().__init__()
self.setGeometry(200,200, 400, 500)
self.container = QWidget()
self.layout = QGridLayout()
self.container.setLayout(self.layout)
self.setCentralWidget(self.container)
for i in range(4):
self.x = DragWidget(self, name = 'widget_'+str(i))
self.layout.addWidget( self.x)
self.x.setObjectName('widget_'+str(i))
self.b = QPushButton('widget___'+str(i))
self.layout_2 = QVBoxLayout()
self.x.setLayout(self.layout_2)
self.layout_2.addWidget(self.b)
self.x.show()
self.installEventFilter(self)
self.setAcceptDrops(True)
self.whereDropped.connect(self.attdeta)
self.dragleaves.connect(self.removeParent)
@pyqtSlot()
def removeParent(self) :
print("\n\nself.dragleaves.connect(self.removeParent))")
pass
@pyqtSlot(int, int)
def attdeta(self, tupx, tupy) :
print("\n\nself.whereDropped.connect(self.attdeta)")
print('tupxy : ', tupx , tupy)
def dropEvent(self, e):
pos = e.pos()
widget = e.source()
print('\n\ndropEvent _________')
print('widget = e.source() :' , widget)
print('e.pos() : ' , pos ,'\n\n')
self.whereDropped.emit(e.pos().x(), e.pos().y())
e.accept()
def dragEnterEvent(self, e):
print('dragEnterEvent _________' , e.source(), '\n')
print('dragEnterEvent _________event-source-name :' ,e.source().objectName())
print('e.pos() : ' , e.pos().x())
print('e.pos() : ' , e.pos().y() ,'\n\n')
e.accept()
def dragLeaveEvent(self, event):
print('\n\nDragLeaveEvent event : ', event)
# print(event.sender()) ### AttributeError: 'QDragLeaveEvent' object has no attribute 'sender'
# print(event.source()) ### AttributeError: 'QDragLeaveEvent' object has no attribute 'source'
print("Drag left at: " + str(self.mapFromGlobal(QCursor.pos())))
print('self.dragleaves.emit()')
self.whereDropped.emit(self.mapFromGlobal(QCursor.pos()).x(), self.mapFromGlobal(QCursor.pos()).y()) ## see [https://stackoverflow.com/questions/50022465/how-do-i-get-the-exit-point-from-a-qdragleaveevent][1]
self.dragleaves.emit()
event.accept()
app = QApplication([])
w = MainWindow()
w.show()
app.exec_()
Any chance of getting sender/sorce/widget of dragLeaveEvent
like for
dragEnterEvent
??? :
def dragEnterEvent(self, e):
print('dragEnterEvent _________' , e.source(), '\n')
print('dragEnterEvent _________event-source-name :' ,e.source().objectName())
print('e.pos() : ' , e.pos().x())
print('e.pos() : ' , e.pos().y() ,'\n\n')
e.accept()
Solution
Since a QDragLeaveEvent
is always preceded by a QDragEnterEvent
, you can store the source widget at the dragEnterEvent()
and use it at the dragLeaveEvent()
.
So, do like this.
...
def dragEnterEvent(self, e):
self.drag_source = e.source()
...
def dragLeaveEvent(self, event):
print('dragLeaveEvent, drag_source:', self.drag_source)
...
...
Answered By - relent95
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.