[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] How to select and test if a QGraphicsItem (or a QGraphicsPixmapItem) has been selected
From:       sw33tz <nyavuz.nm20 () gmail ! com>
Date:       2016-04-26 14:19:26
Message-ID: CAHfkZYkcc7-QdaGeYQyis5Mqz+dpDo64SLUyVin24UXq+CR_AA () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


This is the full code:

#!/usr/bin/python

*import *threading
*import *sys
*from *PyQt4 *import *QtGui, QtCore, uic

*global *host_list, host_cs, switch_cs, connectLine_cs

host_list = []
switch_list = []
hostItem_list = []
switchItem_list = []
lineItem_list = []

host_cs = 0
switch_cs = 0
connectLine_cs = 0

*class **host_Object*(QtGui.QGraphicsPixmapItem, QtGui.QWidget):
    *def *__init__(self, parent=None):
        super(host_Object, self).__init__(parent)
        pixmap = QtGui.QPixmap("host.png")
        self.host_pixItem = QtGui.QGraphicsPixmapItem(pixmap.scaled(30, 30,
QtCore.Qt.KeepAspectRatio))
        self
.host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)
        self.host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)

    *def **mousePressEvent*(self, event):
        *if *self.host_pixItem.isSelected():
            *print *'selected object'


*class **graphicsScene*(QtGui.QGraphicsScene, QtGui.QWidget):
    *def *__init__(self, parent=None):
        super(graphicsScene, self).__init__(parent)
        self.i = 0
        self.j = 0
        self.setSceneRect(-180, -90, 360, 180)
        self.pen = QtGui.QPen(QtCore.Qt.black, 3, QtCore.Qt.SolidLine)
        self.under_mouse = 0


    *def **mousePressEvent*(self, event):
        *global *host_cs

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

        *elif *connectLine_cs == 1:
            self.cursorStartPosition = event.scenePos()
            self.start = QtCore.QPoint(self.cursorStartPosition.x(),self
.cursorStartPosition.y())

    *def **mouseMoveEvent*(self, event):
        *if *self.start:
            self.cursorCurrentPosition = event.scenePos()
            current = QtCore.QPointF(self.cursorCurrentPosition.x(),self
.cursorCurrentPosition.y())
            self.draw_line(current)

    *def **draw_line*(self, pos):
        *try*:

            # remove the old line if exists
            self.removeItem(self.line)
        *except*:


*pass         *self.line = QtGui.QGraphicsLineItem(QtCore.QLineF(self.start,
pos))
        self.line.setPen(self.pen)
        self.addItem(self.line)


    *def **mouseReleaseEvent*(self, event):
        *global *host_cs
        *if *host_cs == 1:
            self.addItem(self.host_item.host_pixItem)
            self.host_item.host_pixItem.setPos(event.scenePos())
            hostItem_list.append(self.host_item.host_pixItem)
            self.i += 1
            host_list.append('h' + str(self.i))


        *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.j += 1
            switch_list.append('s' + str(self.j))
            self.update()
        *elif *connectLine_cs == 1:

            lineItem_list.append(self.line)
            *for *link *in *lineItem_list:
                self.addItem(link)



*class **mininetGUI*(QtGui.QMainWindow, QtGui.QWidget):
    *def *__init__(self):
        super(mininetGUI, self).__init__()
        self.ui = uic.loadUi('minipynet.ui')

        self.ui.actionHost.triggered.connect(self.place_host)
        self.ui.actionSwitch.triggered.connect(self.place_switch)
        self.ui.actionConnectLine.triggered.connect(self.connect_line)

        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

    *def **connect_line*(self):
        *global *host_cs
        *global *switch_cs
        *global *connectLine_cs
        switch_cs = 0
        host_cs = 0
        connectLine_cs = 1
        #self.setMouseTracking(True)



*if *__name__ == '__main__':

    app = QtGui.QApplication(sys.argv)  # A new instance of QApplication
    form = mininetGUI()  # We set the form to be our mininetGUI
    form.ui.show()  # Show the form
    app.exec_()

On Tue, Apr 26, 2016 at 5:29 PM, Nesibe Yavuz <nyavuz.nm20@gmail.com> wrote:

> 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@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=inst \
> > ant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.v \
> > iew.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumb \
> > s=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-tp5189232p5189415.html
 Sent from the PyQt mailing list archive at Nabble.com.


[Attachment #5 (text/html)]

<div dir="ltr">This is the full code:<div><br></div><div>







<p class=""><span class="">#!/usr/bin/python<br>
<br>
</span><span class=""><b>import </b></span>threading<br>
<span class=""><b>import </b></span>sys<br>
<span class=""><b>from </b></span>PyQt4 <span class=""><b>import \
</b></span>QtGui<span class="">, </span>QtCore<span class="">, </span>uic<br> <br>
<span class=""><b>global </b></span>host_list<span class="">, </span>host_cs<span \
class="">, </span>switch_cs<span class="">, </span>connectLine_cs<br> <br>
host_list = []<br>
switch_list = []<br>
hostItem_list = []<br>
switchItem_list = []<br>
lineItem_list = []<br>
<br>
host_cs = <span class="">0<br>
</span>switch_cs = <span class="">0<br>
</span>connectLine_cs = <span class="">0<br>
<br>
</span><span class=""><b>class \
</b></span><b>host_Object</b>(QtGui.QGraphicsPixmapItem<span class="">, \
                </span>QtGui.QWidget):<br>
      <span class=""><b>def </b></span><span class="">__init__</span>(<span \
                class="">self</span><span class="">, </span>parent=<span \
                class="">None</span>):<br>
            <span class="">super</span>(host_Object<span class="">, </span><span \
                class="">self</span>).<span class="">__init__</span>(parent)<br>
            pixmap = QtGui.QPixmap(<span class="">&quot;host.png&quot;</span>)<br>
            <span class="">self</span>.host_pixItem = \
QtGui.QGraphicsPixmapItem(pixmap.scaled(<span class="">30</span><span class="">, \
</span><span class="">30</span><span class="">, \
                </span>QtCore.Qt.KeepAspectRatio))<br>
            <span class="">self</span>.host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)<br>
                
            <span class="">self</span>.host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)<br>
 <br>
      <span class=""><b>def </b></span><b>mousePressEvent</b>(<span \
                class="">self</span><span class="">, </span>event):<br>
            <span class=""><b>if </b></span><span \
                class="">self</span>.host_pixItem.isSelected():<br>
                  <span class=""><b>print </b></span><span class="">&#39;selected \
object&#39;<br> <br>
<br>
</span><span class=""><b>class \
</b></span><b>graphicsScene</b>(QtGui.QGraphicsScene<span class="">, \
                </span>QtGui.QWidget):<br>
      <span class=""><b>def </b></span><span class="">__init__</span>(<span \
                class="">self</span><span class="">, </span>parent=<span \
                class="">None</span>):<br>
            <span class="">super</span>(graphicsScene<span class="">, </span><span \
class="">self</span>).<span class="">__init__</span>(parent)<br>  <span \
class="">self</span>.i = <span class="">0<br>  </span><span class="">self</span>.j = \
<span class="">0<br>  </span><span class="">self</span>.setSceneRect(-<span \
class="">180</span><span class="">, </span>-<span class="">90</span><span class="">, \
</span><span class="">360</span><span class="">, </span><span \
class="">180</span>)<br>  <span class="">self</span>.pen = \
QtGui.QPen(QtCore.Qt.black<span class="">, </span><span class="">3</span><span \
class="">, </span>QtCore.Qt.SolidLine)<br>  <span class="">self</span>.under_mouse = \
<span class="">0<br> <br>
       </span><span class=""><br>
      </span><span class=""><b>def </b></span><b>mousePressEvent</b>(<span \
class="">self</span><span class="">, </span>event):<br>  <span class=""><b>global \
</b></span>host_cs<br> <br>
            <span class=""><b>if </b></span>host_cs == <span class="">1</span>:<br>
                  <span class="">self</span>.host_item = host_Object()<br>
                  <span class=""><b>for </b></span>host <span class=""><b>in \
                </b></span>hostItem_list:<br>
                        <span class=""><b>if </b></span>host.isUnderMouse():<br>
                              <span class=""><b>print </b></span><span \
class="">&#39;selected&#39;<br> <br>
            </span><span class=""><b>elif </b></span>connectLine_cs == <span \
                class="">1</span>:<br>
                  <span class="">self</span>.cursorStartPosition = \
event.scenePos()<br>  <span class="">self</span>.start = QtCore.QPoint(<span \
class="">self</span>.cursorStartPosition.x()<span class="">,</span><span \
class="">self</span>.cursorStartPosition.y())<br> <br>
      <span class=""><b>def </b></span><b>mouseMoveEvent</b>(<span \
                class="">self</span><span class="">, </span>event):<br>
            <span class=""><b>if </b></span><span class="">self</span>.start:<br>
                  <span class="">self</span>.cursorCurrentPosition = \
event.scenePos()<br>  current = QtCore.QPointF(<span \
class="">self</span>.cursorCurrentPosition.x()<span class="">,</span><span \
class="">self</span>.cursorCurrentPosition.y())<br>  <span \
class="">self</span>.draw_line(current)<br> <br>
      <span class=""><b>def </b></span><b>draw_line</b>(<span \
class="">self</span><span class="">, </span>pos):<br>  <span \
class=""><b>try</b></span>:<br> <br>
                  <span class=""># remove the old line if exists<br>
                  </span><span class="">self</span>.removeItem(<span \
class="">self</span>.line)<br>  <span class=""><b>except</b></span>:<br>
                  <span class=""><b>pass<br>
<br>
            </b></span><span class="">self</span>.line = \
QtGui.QGraphicsLineItem(QtCore.QLineF(<span class="">self</span>.start<span \
                class="">, </span>pos))<br>
            <span class="">self</span>.line.setPen(<span \
                class="">self</span>.pen)<br>
            <span class="">self</span>.addItem(<span class="">self</span>.line)<br>
<br>
<br>
      <span class=""><b>def </b></span><b>mouseReleaseEvent</b>(<span \
class="">self</span><span class="">, </span>event):<br>  <span class=""><b>global \
                </b></span>host_cs<br>
            <span class=""><b>if </b></span>host_cs == <span class="">1</span>:<br>
                  <span class="">self</span>.addItem(<span \
                class="">self</span>.host_item.host_pixItem)<br>
                  <span \
                class="">self</span>.host_item.host_pixItem.setPos(event.scenePos())<br>
                
                  hostItem_list.append(<span \
class="">self</span>.host_item.host_pixItem)<br>  <span class="">self</span>.i += \
                <span class="">1<br>
                  </span>host_list.append(<span class="">&#39;h&#39; </span>+ <span \
class="">str</span>(<span class="">self</span>.i))<br> <br>
<br>
            <span class=""><b>elif </b></span>switch_cs == <span \
                class="">1</span>:<br>
                  pixmap = QtGui.QPixmap(<span \
class="">&quot;switch.png&quot;</span>)<br>  switch_pixItem = \
QtGui.QGraphicsPixmapItem(pixmap.scaled(<span class="">30</span><span class="">, \
</span><span class="">30</span><span class="">, \
</span>QtCore.Qt.KeepAspectRatio))<br>  <span \
class="">self</span>.addItem(switch_pixItem)<br>  \
switch_pixItem.setPos(event.scenePos())<br>  \
switchItem_list.append(switch_pixItem)<br> <br>
                  <span class="">self</span>.j += <span class="">1<br>
                  </span>switch_list.append(<span class="">&#39;s&#39; </span>+ <span \
class="">str</span>(<span class="">self</span>.j))<br>  <span \
                class="">self</span>.update()<br>
            <span class=""><b>elif </b></span>connectLine_cs == <span \
class="">1</span>:<br> <br>
                  lineItem_list.append(<span class="">self</span>.line)<br>
                  <span class=""><b>for </b></span>link <span class=""><b>in \
</b></span>lineItem_list:<br>  <span class="">self</span>.addItem(link)<br>
<br>
<br>
<br>
<span class=""><b>class </b></span><b>mininetGUI</b>(QtGui.QMainWindow<span \
                class="">, </span>QtGui.QWidget):<br>
      <span class=""><b>def </b></span><span class="">__init__</span>(<span \
                class="">self</span>):<br>
            <span class="">super</span>(mininetGUI<span class="">, </span><span \
                class="">self</span>).<span class="">__init__</span>()<br>
            <span class="">self</span>.ui = uic.loadUi(<span \
class="">&#39;minipynet.ui&#39;</span>)<br> <br>
            <span class="">self</span>.ui.actionHost.triggered.connect(<span \
                class="">self</span>.place_host)<br>
            <span class="">self</span>.ui.actionSwitch.triggered.connect(<span \
                class="">self</span>.place_switch)<br>
            <span class="">self</span>.ui.actionConnectLine.triggered.connect(<span \
class="">self</span>.connect_line)<br> <br>
            <span class="">self</span>.scene = graphicsScene()<br>
            <span class="">self</span>.ui.view.setScene(<span \
class="">self</span>.scene)<br> <br>
      <span class=""><b>def </b></span><b>place_host</b>(<span \
class="">self</span>):<br>  <span class=""><b>global </b></span>host_cs<br>
            <span class=""><b>global </b></span>switch_cs<br>
            <span class=""><b>global </b></span>connectLine_cs<br>
            host_cs = <span class="">1<br>
            </span>switch_cs = <span class="">0<br>
            </span>connectLine_cs = <span class="">0<br>
<br>
      </span><span class=""><b>def </b></span><b>place_switch</b>(<span \
class="">self</span>):<br>  <span class=""><b>global </b></span>host_cs<br>
            <span class=""><b>global </b></span>switch_cs<br>
            <span class=""><b>global </b></span>connectLine_cs<br>
            switch_cs = <span class="">1<br>
            </span>host_cs = <span class="">0<br>
            </span>connectLine_cs = <span class="">0<br>
<br>
      </span><span class=""><b>def </b></span><b>connect_line</b>(<span \
class="">self</span>):<br>  <span class=""><b>global </b></span>host_cs<br>
            <span class=""><b>global </b></span>switch_cs<br>
            <span class=""><b>global </b></span>connectLine_cs<br>
            switch_cs = <span class="">0<br>
            </span>host_cs = <span class="">0<br>
            </span>connectLine_cs = <span class="">1<br>
            </span><span class="">#self.setMouseTracking(True)<br>
<br>
<br>
<br>
</span><span class=""><b>if </b></span>__name__ == <span \
class="">&#39;__main__&#39;</span>:<br> <br>
      app = QtGui.QApplication(sys.argv)   <span class=""># A new instance of \
                QApplication<br>
      </span>form = mininetGUI()   <span class=""># We set the form to be our \
mininetGUI<br>  </span>form.ui.show()   <span class=""># Show the form<br>
      </span>app.exec_()<br>
</p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 26, \
2016 at 5:29 PM, Nesibe Yavuz <span dir="ltr">&lt;<a \
href="/user/SendEmail.jtp?type=node&node=5189415&i=0" target="_top" rel="nofollow" \
link="external">[hidden email]</a>&gt;</span> wrote:<br><blockquote \
style='border-left:2px solid #CCCCCC;padding:0 1em' class="gmail_quote" \
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div \
dir="ltr">I want to implement your second suggestion :<span \
class=""><div><br></div><div>&quot;<span \
style="color:rgb(80,0,80);font-size:12.8px">2) If you want your host/switch/line \
tools et.c to also double as</span><span style="color:rgb(80,0,80);font-size:12.8px"> \
</span></div><span style="color:rgb(80,0,80);font-size:12.8px">selection tools (e.g. \
click outside an item will create a new item,  </span><br \
style="color:rgb(80,0,80);font-size:12.8px"><span \
style="color:rgb(80,0,80);font-size:12.8px">while click on an item will select it), \
then you need to check if  </span><br \
style="color:rgb(80,0,80);font-size:12.8px"><span \
style="color:rgb(80,0,80);font-size:12.8px">there&#39;s an item under the cursor when \
the click happens, and if there  </span><br \
style="color:rgb(80,0,80);font-size:12.8px"><div><span \
style="color:rgb(80,0,80);font-size:12.8px">is, select it, otherwise go on with your \
current logic.</span><span style="color:rgb(80,0,80);font-size:12.8px">  \
</span>&quot;</div><div><br></div></span><div>And to do that I wrote this \
code:</div><div><br></div><div>







<p><span><b>if </b></span>host_cs == <span>1</span>:<br>
      <span>self</span>.host_item = host_Object()<br>
      <span><b>for </b></span>host <span><b>in </b></span>hostItem_list:<br>
            <span><b>if </b></span>host.isUnderMouse():<br>
                  <span><b>print </b></span><span>&#39;under \
mouse&#39;</span></p><p>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&#39;t drawn over each other (for when trying \
to select an item the host_item keeps on being added to the scene):</p><p>







</p><p><span><b>if </b></span>host_cs == <span>1</span>:<br>
      <span>self</span>.host_item = host_Object()<br>
      <span><b>for </b></span>host <span><b>in </b></span>hostItem_list:<br>          \
<span><b>if </b></span><span>self</span>.host_item.host_pixItem.collidesWithItem(host):</p><p> \
<span><b>print </b></span><span>&#39;collision&#39;</span></p><p>Here the \
&quot;<span><b>if  </b></span><span>self</span>.host_item.host_pixItem.collidesWithItem(host):&quot; \
statement keeps giving &quot;False&quot; as the output even though when I try to add \
an item over another item the statement clearly should return \
&quot;True&quot;.</p><p><br></p><p>By the way I&#39;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.</p><p><br></p><p>  </p></div></div><div \
class="gmail_extra"><br><div class="gmail_quote">On Mon, Apr 25, 2016 at 9:07 PM, \
Elvis Stansvik [via Python] <span dir="ltr">&lt;<a \
href="/user/SendEmail.jtp?type=node&node=5189415&i=1" target="_top" rel="nofollow" \
link="external">[hidden email]</a>&gt;</span> wrote:<br><blockquote \
style='border-left:2px solid #CCCCCC;padding:0 1em' class="gmail_quote" \
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div \
class="h5"><div><div>

	2016-04-25 19:27 GMT+02:00 sw33tz &lt;<a \
href="http:///user/SendEmail.jtp?type=node&amp;node=5189328&amp;i=0" rel="nofollow" \
link="external" target="_blank">[hidden email]</a>&gt;: <br>&gt; thank you for the \
reply. I am able to select the item when there is only one <br>&gt; item but I want \
the user to be able to add more than one item and also <br>&gt; select any item that \
they added. With my code I can&#39;t do that because with <br>&gt; every press event \
it just keeps adding items. Is there a way I can make this <br>&gt; happen?
<br><br>For the first question, I think the default behavior is that you can
<br>select multiple items by holding down Ctrl. If you want rubber band
<br>selection, you can do it quite easily by setting the drag mode of the
<br>QGraphicsView. If you want some more customized selection behavior, I
<br>think you&#39;ll have to manually handle the mouse/keyboard events and
<br>call setSelected on the items you want to select.
<br><br>For the second question: It again depends on what behavior you want:
<br><br>1) If you want to have a &quot;select tool&quot; which the user must choose
<br>before he/she can select, then I guess you would implement it
<br>similarly to how you&#39;ve implemented the host/switch/line tools that
<br>you&#39;ve shown. You would have an elif for your select tool, where you
<br>let QGraphicsScene (the base class) handle the event, this will
<br>forward the event to the item (instead of it being swallowed by the
<br>scene event handler like you did, and the item will select itself.
<br><br>2) If you want your host/switch/line tools et.c to also double as
<br>selection tools (e.g. click outside an item will create a new item,
<br>while click on an item will select it), then you need to check if
<br>there&#39;s an item under the cursor when the click happens, and if there
<br>is, select it, otherwise go on with your current logic.
<br><br>This is all from memory, so better check the docs. But I hope it gets
<br>you started.
<br><br>Googling also gives a lot of hits about selection handling with
<br>QGraphicsScene/QGraphicsView.
<br><br>Elvis
<br><br>&gt;
<br>&gt;
<br>&gt;
<br></div></div>&gt; --
<br>&gt; 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-tp5189232p5189325.html" \
rel="nofollow" link="external" \
target="_blank">http://python.6.x6.nabble.com/How-to-select-and-test-if-a-QGraphicsIte \
m-or-a-QGraphicsPixmapItem-has-been-selected-tp5189232p5189325.html</a><span><br>&gt; \
Sent from the PyQt mailing list archive at Nabble.com. <br>&gt; \
_______________________________________________ <br></span></div></div><span \
class="">&gt; PyQt mailing list      <a \
href="http:///user/SendEmail.jtp?type=node&amp;node=5189328&amp;i=1" rel="nofollow" \
link="external" target="_blank">[hidden email]</a> <br>&gt; <a \
href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="nofollow" \
link="external" target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>_______________________________________________
 <br>PyQt mailing list      <a \
href="http:///user/SendEmail.jtp?type=node&amp;node=5189328&amp;i=2" rel="nofollow" \
link="external" target="_blank">[hidden email]</a> <br><a \
href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="nofollow" \
link="external" target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a>


	
	
	
	<br>
	<br>
	<hr noshade size="1" color="#cccccc">
	<div style="color:#444;font:12px tahoma,geneva,helvetica,arial,sans-serif">
		<div style="font-weight:bold">If you reply to this email, your message will be \
added to the discussion below:</div>  <a \
href="http://python.6.x6.nabble.com/How-to-select-and-test-if-a-QGraphicsItem-or-a-QGraphicsPixmapItem-has-been-selected-tp5189232p5189328.html" \
target="_blank" rel="nofollow" \
link="external">http://python.6.x6.nabble.com/How-to-select-and-test-if-a-QGraphicsItem-or-a-QGraphicsPixmapItem-has-been-selected-tp5189232p5189328.html</a>
  </div>
	<div style="color:#666;font:11px \
tahoma,geneva,helvetica,arial,sans-serif;margin-top:.4em;line-height:1.5em">  
		To unsubscribe from How to select and test if a QGraphicsItem (or a \
QGraphicsPixmapItem) has been selected, <a href="" target="_blank" rel="nofollow" \
link="external">click here</a>.<br>  <a \
href="http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&amp;id \
=instant_html%21nabble%3Aemail.naml&amp;base=nabble.naml.namespaces.BasicNamespace-nab \
ble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&amp;bread \
crumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml" \
rel="nofollow" style="font:9px serif" target="_blank" link="external">NAML</a>  \
</div></span></blockquote></div><br></div> </blockquote></div><br></div>


	
	
	
<br/><hr align="left" width="300" />
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-tp5189232p5189415.html">Re: \
How to select and test if a QGraphicsItem (or a QGraphicsPixmapItem) has been \
selected</a><br/> Sent from the <a \
href="http://python.6.x6.nabble.com/PyQt-f1792048.html">PyQt mailing list archive</a> \
at Nabble.com.<br/>


[Attachment #6 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic