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

sw33tz nyavuz.nm20 at gmail.com
Tue Apr 26 15:14:54 BST 2016


I want to implement your second suggestion :

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

And to do that I wrote this code:

*if *host_cs == 1:
    self.host_item = host_Object()
    *for *host *in *hostItem_list:
        *if *host.isUnderMouse():
            *print *'under mouse'

The problem here is that I can only select the first item I ever create and
I have to keep on pressing the item (maybe about 4-5 times) until it can
actually be selected. I tried using the isSelected() method to check if the
item was selected and I still get the same result. I also tried to test for
a collision when adding the items so the items aren't drawn over each other
(for when trying to select an item the host_item keeps on being added to
the scene):

*if *host_cs == 1:
    self.host_item = host_Object()
    *for *host *in *hostItem_list:
         *if *self.host_item.host_pixItem.collidesWithItem(host):

              *print *'collision'

Here the "*if *self.host_item.host_pixItem.collidesWithItem(host):"
statement keeps giving "False" as the output even though when I try to add
an item over another item the statement clearly should return "True".


By the way I'm sorry for the late replies I try to take your suggestions in
consideration and I try to implement them before replying back to you.




On Mon, Apr 25, 2016 at 9:07 PM, Elvis Stansvik [via Python] <
ml-node+s6n5189328h79 at n6.nabble.com> wrote:

> 2016-04-25 19:27 GMT+02:00 sw33tz <[hidden email]
> <http:///user/SendEmail.jtp?type=node&node=5189328&i=0>>:
> > 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    [hidden email]
> <http:///user/SendEmail.jtp?type=node&node=5189328&i=1>
> > https://www.riverbankcomputing.com/mailman/listinfo/pyqt
> _______________________________________________
> PyQt mailing list    [hidden email]
> <http:///user/SendEmail.jtp?type=node&node=5189328&i=2>
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://python.6.x6.nabble.com/How-to-select-and-test-if-a-QGraphicsItem-or-a-QGraphicsPixmapItem-has-been-selected-tp5189232p5189328.html
> To unsubscribe from How to select and test if a QGraphicsItem (or a
> QGraphicsPixmapItem) has been selected, click here
> <http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5189232&code=bnlhdnV6Lm5tMjBAZ21haWwuY29tfDUxODkyMzJ8LTE2MTY4NDE4NTE=>
> .
> NAML
> <http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
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-tp5189232p5189412.html
Sent from the PyQt mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160426/b25a4312/attachment.html>


More information about the PyQt mailing list