[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    [PyQt] collidesWithItem() function not detecting collision
From:       sw33tz <nyavuz.nm20 () gmail ! com>
Date:       2016-04-26 19:17:23
Message-ID: 1461698243764-5189439.post () n6 ! nabble ! com
[Download RAW message or body]

I'm writing an application that allows the user to add items to a scene. I
dont want any new items being drawn over items that have already been drawn
and to do that I decided to use the collidesWithItem() function to detect
collision. With my code i still can draw over added items even though there
is obviously collision and I debugged the program and the collidesWithItem()
function keeps returning "False".

The items are added by clicking on the toolbar of the Form.

Down below is my code:

class graphicsScene(QtGui.QGraphicsScene, QtGui.QWidget):
    def __init__(self, parent=None):
        super(graphicsScene, self).__init__(parent)
        self.i = 0
        self.setSceneRect(-180, -90, 360, 180)
        global overlapped
        overlapped = 0

    def mousePressEvent(self, event):
        global host_cs
        global overlapped

        if host_cs == 1:
            if len(hostItem_list) == 0:
                self.host_item = host_Object()
                hostItem_list.append(self.host_item.host_pixItem)
            else:
                self.host_item = host_Object()
                for host in hostItem_list:
                    if self.host_item.host_pixItem.collidesWithItem(host):
                    print 'collision'
                    overlapped = 1
                    break

                    elif self.host_item.host_pixItem.collidesWithItem(host)
== False:
                        overlapped = 0
                if overlapped == 0:
                    hostItem_list.append(self.host_item.host_pixItem)

    def mouseReleaseEvent(self, event):
        global host_cs
        if host_cs == 1:
            if overlapped == 0:
                self.addItem(self.host_item.host_pixItem)
                self.host_item.host_pixItem.setPos(event.scenePos())
                self.i += 1
                host_list.append('h' + str(self.i))

class host_Object(QtGui.QGraphicsPixmapItem, QtGui.QWidget):
    def __init__(self, parent=None):
        super(host_Object, self).__init__(parent)
        pixmap = QtGui.QPixmap("host.png")
        self.host_pixItem = QtGui.QGraphicsPixmapItem(pixmap.scaled(30, 30,
QtCore.Qt.KeepAspectRatio))
       
self.host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)
        self.host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)

class Form(QtGui.QMainWindow, QtGui.QWidget):
    def __init__(self):
        super(Form, self).__init__()
        self.ui = uic.loadUi('form.ui')

        self.ui.actionHost.triggered.connect(self.place_host)

        self.scene = graphicsScene()
        self.ui.view.setScene(self.scene)

    def place_host(self):
        host_cs = 1

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)  
    form = Form() 
    form.ui.show()  
    app.exec_()



--
View this message in context: \
http://python.6.x6.nabble.com/collidesWithItem-function-not-detecting-collision-tp5189439.html
 Sent from the PyQt mailing list archive at Nabble.com.
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic