<div dir="ltr">Hello there, I sent you two emails, I dont know if you saw but I would very much appreciate it if you took a look them :)</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 26, 2016 at 5:33 PM, Nesibe Yavuz <span dir="ltr"><<a href="/user/SendEmail.jtp?type=node&node=5189804&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">This is the full code:<div><br></div><div>
<p><span>#!/usr/bin/python<br>
<br>
</span><span><b>import </b></span>threading<br>
<span><b>import </b></span>sys<br>
<span><b>from </b></span>PyQt4 <span><b>import </b></span>QtGui<span>, </span>QtCore<span>, </span>uic<br>
<br>
<span><b>global </b></span>host_list<span>, </span>host_cs<span>, </span>switch_cs<span>, </span>connectLine_cs<br>
<br>
host_list = []<br>
switch_list = []<br>
hostItem_list = []<br>
switchItem_list = []<br>
lineItem_list = []<br>
<br>
host_cs = <span>0<br>
</span><span class="">switch_cs = <span>0<br>
</span>connectLine_cs = <span>0<br>
<br>
</span></span><span><b>class </b></span><b>host_Object</b>(QtGui.QGraphicsPixmapItem<span>, </span>QtGui.QWidget):<br>
<span><b>def </b></span><span>__init__</span>(<span>self</span><span>, </span>parent=<span>None</span>):<br>
<span>super</span>(host_Object<span>, </span><span>self</span>).<span>__init__</span>(parent)<span class=""><br>
pixmap = QtGui.QPixmap(<span>"host.png"</span>)<br></span>
<span>self</span>.host_pixItem = QtGui.QGraphicsPixmapItem(pixmap.scaled(<span>30</span><span>, </span><span>30</span><span>, </span>QtCore.Qt.KeepAspectRatio))<br>
<span>self</span>.host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)<br>
<span>self</span>.host_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)<br>
<br>
<span><b>def </b></span><b>mousePressEvent</b>(<span>self</span><span>, </span>event):<br>
<span><b>if </b></span><span>self</span>.host_pixItem.isSelected():<br>
<span><b>print </b></span><span>'selected object'<br>
<br>
<br>
</span><span class=""><span><b>class </b></span><b>graphicsScene</b>(QtGui.QGraphicsScene<span>, </span>QtGui.QWidget):<br>
<span><b>def </b></span><span>__init__</span>(<span>self</span><span>, </span>parent=<span>None</span>):<br>
<span>super</span>(graphicsScene<span>, </span><span>self</span>).<span>__init__</span>(parent)<br></span>
<span>self</span>.i = <span>0<br>
</span><span>self</span>.j = <span>0<br>
</span><span>self</span>.setSceneRect(-<span>180</span><span>, </span>-<span>90</span><span>, </span><span>360</span><span>, </span><span>180</span>)<br>
<span>self</span>.pen = QtGui.QPen(QtCore.Qt.black<span>, </span><span>3</span><span>, </span>QtCore.Qt.SolidLine)<br>
<span>self</span>.under_mouse = <span>0<br>
<br>
</span><span><br>
</span><span><b>def </b></span><b>mousePressEvent</b>(<span>self</span><span>, </span>event):<br>
<span><b>global </b></span>host_cs<span class=""><br>
<br>
<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>
<span><b>print </b></span><span>'selected'<br>
<br>
</span><span><b>elif </b></span>connectLine_cs == <span>1</span>:<br>
<span>self</span>.cursorStartPosition = event.scenePos()<br>
<span>self</span>.start = QtCore.QPoint(<span>self</span>.cursorStartPosition.x()<span>,</span><span>self</span>.cursorStartPosition.y())<br>
<br>
<span><b>def </b></span><b>mouseMoveEvent</b>(<span>self</span><span>, </span>event):<br>
<span><b>if </b></span><span>self</span>.start:<br>
<span>self</span>.cursorCurrentPosition = event.scenePos()<br>
current = QtCore.QPointF(<span>self</span>.cursorCurrentPosition.x()<span>,</span><span>self</span>.cursorCurrentPosition.y())<br>
<span>self</span>.draw_line(current)<br>
<br>
<span><b>def </b></span><b>draw_line</b>(<span>self</span><span>, </span>pos):<br>
<span><b>try</b></span>:<br>
<br>
<span># remove the old line if exists<br>
</span><span>self</span>.removeItem(<span>self</span>.line)<br>
<span><b>except</b></span>:<br>
<span><b>pass<br>
<br>
</b></span><span>self</span>.line = QtGui.QGraphicsLineItem(QtCore.QLineF(<span>self</span>.start<span>, </span>pos))<br>
<span>self</span>.line.setPen(<span>self</span>.pen)<br>
<span>self</span>.addItem(<span>self</span>.line)<br>
<br>
<br>
<span><b>def </b></span><b>mouseReleaseEvent</b>(<span>self</span><span>, </span>event):<br>
<span><b>global </b></span>host_cs<br>
<span><b>if </b></span>host_cs == <span>1</span>:<br>
<span>self</span>.addItem(<span>self</span>.host_item.host_pixItem)<br>
<span>self</span>.host_item.host_pixItem.setPos(event.scenePos())<br>
hostItem_list.append(<span>self</span>.host_item.host_pixItem)<br>
<span>self</span>.i += <span>1<br>
</span>host_list.append(<span>'h' </span>+ <span>str</span>(<span>self</span>.i))<span class=""><br>
<br>
<br>
<span><b>elif </b></span>switch_cs == <span>1</span>:<br>
pixmap = QtGui.QPixmap(<span>"switch.png"</span>)<br>
switch_pixItem = QtGui.QGraphicsPixmapItem(pixmap.scaled(<span>30</span><span>, </span><span>30</span><span>, </span>QtCore.Qt.KeepAspectRatio))<br>
<span>self</span>.addItem(switch_pixItem)<br>
switch_pixItem.setPos(event.scenePos())<br>
switchItem_list.append(switch_pixItem)<br>
<br></span>
<span>self</span>.j += <span>1<br>
</span>switch_list.append(<span>'s' </span>+ <span>str</span>(<span>self</span>.j))<br>
<span>self</span>.update()<br>
<span><b>elif </b></span>connectLine_cs == <span>1</span>:<br>
<br>
lineItem_list.append(<span>self</span>.line)<br>
<span><b>for </b></span>link <span><b>in </b></span>lineItem_list:<br>
<span>self</span>.addItem(link)<br>
<br>
<br>
<br>
<span><b>class </b></span><b>mininetGUI</b>(QtGui.QMainWindow<span>, </span>QtGui.QWidget):<br>
<span><b>def </b></span><span>__init__</span>(<span>self</span>):<br>
<span>super</span>(mininetGUI<span>, </span><span>self</span>).<span>__init__</span>()<br>
<span>self</span>.ui = uic.loadUi(<span>'minipynet.ui'</span>)<br>
<br>
<span>self</span>.ui.actionHost.triggered.connect(<span>self</span>.place_host)<br>
<span>self</span>.ui.actionSwitch.triggered.connect(<span>self</span>.place_switch)<br>
<span>self</span>.ui.actionConnectLine.triggered.connect(<span>self</span>.connect_line)<span class=""><br>
<br>
<span>self</span>.scene = graphicsScene()<br>
<span>self</span>.ui.view.setScene(<span>self</span>.scene)<br>
<br>
<span><b>def </b></span><b>place_host</b>(<span>self</span>):<br>
<span><b>global </b></span>host_cs<br>
<span><b>global </b></span>switch_cs<br>
<span><b>global </b></span>connectLine_cs<br>
host_cs = <span>1<br>
</span>switch_cs = <span>0<br>
</span>connectLine_cs = <span>0<br>
<br>
</span><span><b>def </b></span><b>place_switch</b>(<span>self</span>):<br>
<span><b>global </b></span>host_cs<br>
<span><b>global </b></span>switch_cs<br>
<span><b>global </b></span>connectLine_cs<br>
switch_cs = <span>1<br>
</span>host_cs = <span>0<br>
</span>connectLine_cs = </span><span>0<br>
<br>
</span><span><b>def </b></span><b>connect_line</b>(<span>self</span>):<span class=""><br>
<span><b>global </b></span>host_cs<br>
<span><b>global </b></span>switch_cs<br>
<span><b>global </b></span>connectLine_cs<br></span>
switch_cs = <span>0<br>
</span>host_cs = <span>0<br>
</span>connectLine_cs = <span>1<br>
</span><span>#self.setMouseTracking(True)<br>
<br>
<br>
<br>
</span><span><b>if </b></span>__name__ == <span>'__main__'</span>:<br>
<br>
app = QtGui.QApplication(sys.argv) <span># A new instance of QApplication<br>
</span>form = mininetGUI() <span># We set the form to be our mininetGUI<br>
</span>form.ui.show() <span># Show the form<br>
</span>app.exec_()<br>
</p></div></div><div class="HOEnZb"><div class="h5"><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=5189804&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 dir="ltr">I want to implement your second suggestion :<span><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=5189804&i=2" 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><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>> 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>
</div></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-tp5189232p5189804.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/>