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

Elvis Stansvik elvstone at gmail.com
Mon Apr 25 07:35:49 BST 2016


Den 24 apr 2016 2:22 em skrev "sw33tz" <nyavuz.nm20 at gmail.com>:
>
> 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)

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.

Hope that helps.

Elvis

>              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.
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160425/1859d602/attachment.html>


More information about the PyQt mailing list