<p dir="ltr"><br>
Den 24 apr 2016 2:22 em skrev "sw33tz" <<a href="mailto:nyavuz.nm20@gmail.com">nyavuz.nm20@gmail.com</a>>:<br>
><br>
> How can I select a QGraphicsItem and test to see if it has been selected? My<br>
> program allows the user to add items to the QGraphicsScene (by clicking from<br>
> the toolbar and then adding it to the scene). When the user clicks from the<br>
> toolbar it lets the user add as many graphical items as he or she wants to<br>
> the scene. I want the user to be able to select an item that was added and<br>
> be able to move it. How would I go about doing that? Down below is my code:<br>
><br>
> class Form(QtGui.QMainWindow, QtGui.QWidget):<br>
> def __init__(self):<br>
> super(Form, self).__init__()<br>
> self.ui = uic.loadUi('Form.ui')<br>
><br>
> self.ui.actionHost.triggered.connect(self.place_host)<br>
> self.ui.actionSwitch.triggered.connect(self.place_switch)<br>
><br>
> self.scene = graphicsScene()<br>
> self.ui.view.setScene(self.scene)<br>
><br>
> def place_host(self):<br>
> global host_cs<br>
> global switch_cs<br>
> global connectLine_cs<br>
> host_cs = 1<br>
> switch_cs = 0<br>
> connectLine_cs = 0<br>
><br>
> def place_switch(self):<br>
> global host_cs<br>
> global switch_cs<br>
> global connectLine_cs<br>
> switch_cs = 1<br>
> host_cs = 0<br>
> connectLine_cs = 0<br>
><br>
> class graphicsScene(QtGui.QGraphicsScene, QtGui.QWidget):<br>
> def __init__(self, parent=None):<br>
> super(graphicsScene, self).__init__(parent)<br>
> self.setSceneRect(-180, -90, 360, 180)<br>
><br>
> def mouseReleaseEvent(self, event):<br>
><br>
> if host_cs == 1:<br>
> pixmap = QtGui.QPixmap("host.png")<br>
> host_pixItem = QtGui.QGraphicsPixmapItem(pixmap.scaled(30, 30,<br>
> QtCore.Qt.KeepAspectRatio))<br>
><br>
> host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)<br>
> host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)</p>
<p dir="ltr">This is correct. The item should now be selectable and movable. Note that if you want to paint the item differently when selected, you must subclass QGraphicsPixmapItem and override the paint method. In it, you can first call the base class paint method, then check if selected with isSelected before painting your selection overlay.</p>
<p dir="ltr">Hope that helps.</p>
<p dir="ltr">Elvis</p>
<p dir="ltr">> self.addItem(host_pixItem)<br>
> host_pixItem.setPos(event.scenePos())<br>
> hostItem_list.append(host_pixItem)<br>
> self.update()<br>
><br>
> elif switch_cs == 1:<br>
> pixmap = QtGui.QPixmap("switch.png")<br>
> switch_pixItem = QtGui.QGraphicsPixmapItem(pixmap.scaled(30, 30,<br>
> QtCore.Qt.KeepAspectRatio))<br>
> self.addItem(switch_pixItem)<br>
> switch_pixItem.setPos(event.scenePos())<br>
> switchItem_list.append(switch_pixItem)<br>
> self.update()<br>
><br>
><br>
><br>
> --<br>
> View this message in context: <a href="http://python.6.x6.nabble.com/How-to-select-and-test-if-a-QGraphicsItem-or-a-QGraphicsPixmapItem-has-been-selected-tp5189232.html">http://python.6.x6.nabble.com/How-to-select-and-test-if-a-QGraphicsItem-or-a-QGraphicsPixmapItem-has-been-selected-tp5189232.html</a><br>
> Sent from the PyQt mailing list archive at Nabble.com.<br>
> _______________________________________________<br>
> PyQt mailing list <a href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a><br>
> <a href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a></p>