Drag&Drap: Quelle von answerRect ?

Python und das Qt-Toolkit, erstellen von GUIs mittels des Qt-Designers.
Antworten
schnumbl
User
Beiträge: 6
Registriert: Montag 9. Januar 2012, 21:30

Hallo, ich versuche mich gerade an Drag&Drop.
Wie kann ich unterscheiden ob ein answerRect von meinem Target-Objekt
oder von einem Child des Target-Objekts kommt?

Bild

Code: Alles auswählen

#!/usr/bin/python
# -*- coding: utf-8 -*-



import sys

from PyQt4 import QtCore
from PyQt4.QtCore import *
from PyQt4 import QtGui
from PyQt4 import QtSvg


        
class dragdrop_target(QtGui.QFrame):
    
    def __init__(self, ParentWidget, SvgString=""):
        super(dragdrop_target, self).__init__(ParentWidget)
        
        self.xpx = 200
        self.ypx = 200
        self.widthpx = 200
        self.heightpx = 100
      
        self.setAcceptDrops(True)
        self.setStyleSheet("color:green")
        self.setFrameStyle(1)
        self.resize(self.widthpx,self.heightpx)
        self.move(self.xpx,self.ypx)
        
        self.child = QtGui.QFrame(self)
        self.child.setAcceptDrops(False) #<=this does not disable the answerRect firing of the child
        self.child.setStyleSheet("color:red")
        self.child.setFrameStyle(1)
        self.child.resize(20,20)
        self.child.move(100,80)
        
    def mouseMoveEvent(self, e):
        if e.buttons() != QtCore.Qt.LeftButton:
            return
        mimeData = QtCore.QMimeData()
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setHotSpot(e.pos() - self.rect().topLeft())
        drag.start(QtCore.Qt.MoveAction)         
            
    def dragEnterEvent(self, e):        
        if e.source() != self:
            e.accept()
            print "target_dragEnter"
        else:
            e.ignore()
            print "target_dragEnterIgnore"            
            
    def dragMoveEvent(self, e):
        if e.source() != self:
            e.accept()
            print "target_dragMove"
            
            #print e.answerRect()
            posx = e.answerRect().x()
            posy = e.answerRect().y()  
            
            print "x: " + str(posx) + "y: " + str(posy)
            print e.source()
            
            # case1: answerRect is from the target-object => all works fine
            # case2: answerRect is from child of target-object => "northeast" is
            # shown instead of "south" if source object is dragged over the child

            if posx >= 0 and posx <= self.width() and posy >= 0 and posy <= self.height():
                #source-object is moved inside target object
                if posx >= self.width()/3 and posx <= self.width()*2/3 and posy <= self.height()*2/3:
                    print "north"
                elif posx >= self.width()/3 and posx <= self.width()*2/3 and posy >= self.height()*2/3:
                    print "south"
                elif posx <= self.width()/3 and posy >= self.height()/3 and posy <= self.height()*2/3:
                    print "west"
                elif posx >= self.width()*2/3 and posy >= self.height()/3 and posy <= self.height()*2/3:
                    print "east"     
                elif posx < self.width()/3 and posy < self.height()/3:
                    print "northwest"
                elif posx < self.width()/3 and posy > self.height()*2/3:                    
                    print "southwest"                
                elif posx > self.width()*2/3 and posy < self.height()/3:
                    print "northeast"
                elif posx > self.width()*2/3 and posy > self.height()*2/3:
                    print "southeast"  
        else:
            e.ignore()
            print "target_dragMoveIgnore"
                              

    def dropEvent(self, e):
        if e.source() != self:
            e.accept()
            print "target_drop"
        else:
            e.ignore()
            print "target_dropIgnore"
        
    def dragLeaveEvent(self, e):      
        e.accept()
        print "target_dragLeave"   


class dragdrop_source(QtGui.QFrame):
    
    def __init__(self, ParentWidget, SvgString=""):
        super(dragdrop_source, self).__init__(ParentWidget)
      
        self.setAcceptDrops(True)
        self.setStyleSheet("color:blue")
        self.setFrameStyle(1)
        self.resize(40,40)
        self.move(150,100)
        
        
    def mouseMoveEvent(self, e):
        if e.buttons() != QtCore.Qt.LeftButton:
            return
        mimeData = QtCore.QMimeData()
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setHotSpot(e.pos() - self.rect().topLeft())
        drag.start(QtCore.Qt.MoveAction)         
            
    def dragEnterEvent(self, e):        
        if e.source() != self:
            e.accept()
            print "source_dragEnter"
        else:
            e.ignore()
            print "source_dragEnterIgnore"            
            
    def dragMoveEvent(self, e):
        if e.source() != self:
            e.accept()
            print "source_dragMove"           
        else:
            e.ignore()
            print "source_dragMoveIgnore"
                              

    def dropEvent(self, e):
        if e.source() != self:
            e.accept()
            print "source_drop"
        else:
            e.ignore()
            print "source_dropIgnore"
        
    def dragLeaveEvent(self, e):      
        e.accept()
        print "source_dragLeave"             

class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def dragEnterEvent(self, e):      
        e.accept()
        print "panelDragEnter"

    def dropEvent(self, e):
        position = e.pos()            
        e.source().move(position)
        e.setDropAction(QtCore.Qt.MoveAction)
        e.accept()
        print "panelDrop"
        
    def initUI(self):
        
        self.setGeometry(200, 200, 600, 600)        
        self.setAcceptDrops(True)
        
        self.targetObject = dragdrop_target(self)  
        
        self.sourceObject = dragdrop_source(self) 
        
        #Set BackgroundColor
        colorstr = 'white'
        widget = self
        pal = QtGui.QPalette(widget.palette())        
        pal.setColor(QtGui.QPalette.Background, QtGui.QColor(colorstr))
        widget.setPalette(pal)           
        
          
        self.show()
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    #app.setOverrideCursor()

    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
schnumbl
User
Beiträge: 6
Registriert: Montag 9. Januar 2012, 21:30

Konnte das Problem mit e.pos() anstelle von e.answerRect() lösen.
Antworten