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

Elvis Stansvik elvstone at gmail.com
Mon Apr 25 19:20:32 BST 2016


2016-04-25 19:27 GMT+02:00 sw33tz <nyavuz.nm20 at gmail.com>:
> thank you for the reply. I am able to select the item when there is only one
> item but I want the user to be able to add more than one item and also
> select any item that they added. With my code I can't do that because with
> every press event it just keeps adding items. Is there a way I can make this
> happen?

For the first question, I think the default behavior is that you can
select multiple items by holding down Ctrl. If you want rubber band
selection, you can do it quite easily by setting the drag mode of the
QGraphicsView. If you want some more customized selection behavior, I
think you'll have to manually handle the mouse/keyboard events and
call setSelected on the items you want to select.

For the second question: It again depends on what behavior you want:

1) If you want to have a "select tool" which the user must choose
before he/she can select, then I guess you would implement it
similarly to how you've implemented the host/switch/line tools that
you've shown. You would have an elif for your select tool, where you
let QGraphicsScene (the base class) handle the event, this will
forward the event to the item (instead of it being swallowed by the
scene event handler like you did, and the item will select itself.

2) If you want your host/switch/line tools et.c to also double as
selection tools (e.g. click outside an item will create a new item,
while click on an item will select it), then you need to check if
there's an item under the cursor when the click happens, and if there
is, select it, otherwise go on with your current logic.

This is all from memory, so better check the docs. But I hope it gets
you started.

Googling also gives a lot of hits about selection handling with
QGraphicsScene/QGraphicsView.

Elvis

>
>
>
> --
> 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-tp5189232p5189325.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


More information about the PyQt mailing list