[PyQt] How to select and test if a QGraphicsItem (or a QGraphicsPixmapItem) has been selected

sw33tz nyavuz.nm20 at gmail.com
Sun Apr 24 13:06:59 BST 2016


How can I select a QGraphicsItem and test to see if it has been selected? My
program allows the user to add items to the QGraphicsScene (by clicking from
the toolbar and then adding it to the scene). When the user clicks from the
toolbar it lets the user add as many graphical items as he or she wants to
the scene. I want the user to be able to select an item that was added and
be able to move it. How would I go about doing that? Down below is my code:

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.ui.actionSwitch.triggered.connect(self.place_switch)

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

    def place_host(self):
        global host_cs
        global switch_cs
        global connectLine_cs
        host_cs = 1
        switch_cs = 0
        connectLine_cs = 0

    def place_switch(self):
        global host_cs
        global switch_cs
        global connectLine_cs
        switch_cs = 1
        host_cs = 0
        connectLine_cs = 0

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

     def mouseReleaseEvent(self, event):

         if host_cs == 1:
             pixmap = QtGui.QPixmap("host.png")
             host_pixItem = QtGui.QGraphicsPixmapItem(pixmap.scaled(30, 30,
QtCore.Qt.KeepAspectRatio))
            
host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)
             host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)
             self.addItem(host_pixItem)
             host_pixItem.setPos(event.scenePos())
             hostItem_list.append(host_pixItem)
             self.update()

         elif switch_cs == 1:
            pixmap = QtGui.QPixmap("switch.png")
            switch_pixItem = QtGui.QGraphicsPixmapItem(pixmap.scaled(30, 30,
QtCore.Qt.KeepAspectRatio))
            self.addItem(switch_pixItem)
            switch_pixItem.setPos(event.scenePos())
            switchItem_list.append(switch_pixItem)
            self.update()



--
View this message in context: http://python.6.x6.nabble.com/How-to-select-and-test-if-a-QGraphicsItem-or-a-QGraphicsPixmapItem-has-been-selected-tp5189232.html
Sent from the PyQt mailing list archive at Nabble.com.


More information about the PyQt mailing list