<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="">"host.png"</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="">'selected object'<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="">'selected'<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="">'h' </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="">"switch.png"</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="">'s' </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="">'minipynet.ui'</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="">'__main__'</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"><<a href="/user/SendEmail.jtp?type=node&node=5189415&i=0" target="_top" rel="nofollow" link="external">[hidden email]</a>></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>"<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'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>"</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>'under mouse'</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'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>'collision'</span></p><p>Here the "<span><b>if </b></span><span>self</span>.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".</p><p><br></p><p>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.</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"><<a href="/user/SendEmail.jtp?type=node&node=5189415&i=1" target="_top" rel="nofollow" link="external">[hidden email]</a>></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 <<a href="http:///user/SendEmail.jtp?type=node&node=5189328&i=0" rel="nofollow" link="external" target="_blank">[hidden email]</a>>:
<br>> thank you for the reply. I am able to select the item when there is only one
<br>> item but I want the user to be able to add more than one item and also
<br>> select any item that they added. With my code I can't do that because with
<br>> every press event it just keeps adding items. Is there a way I can make this
<br>> 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'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 "select tool" which the user must choose
<br>before he/she can select, then I guess you would implement it
<br>similarly to how you've implemented the host/switch/line tools that
<br>you'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'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>>
<br>>
<br>>
<br></div></div>> --
<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-tp5189232p5189325.html" rel="nofollow" link="external" target="_blank">http://python.6.x6.nabble.com/How-to-select-and-test-if-a-QGraphicsItem-or-a-QGraphicsPixmapItem-has-been-selected-tp5189232p5189325.html</a><span><br>> Sent from the PyQt mailing list archive at Nabble.com.
<br>> _______________________________________________
<br></span></div></div><span class="">> PyQt mailing list    <a href="http:///user/SendEmail.jtp?type=node&node=5189328&i=1" 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>PyQt mailing list    <a href="http:///user/SendEmail.jtp?type=node&node=5189328&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&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" 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/>