I have a QGIS python plugin which works fine when toggle button is checked but when i unchecked it then its not closing the socket why?
here is my code
def show_markers(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname('192.168.225.12')
s.connect((host, port))
scale=0
while True:
if self.iconAction.isChecked():
print ('Checked')
m = QgsVertexMarker(self.iface.mapCanvas())
data = s.recv(SIZE)
data1 = s.recv(SIZE)
c = data.decode()
d = data1.decode()
x = float(c)
y = float(d)
print("printing X :", x)
print("printing Y :", y)
rect = QgsRectangle(float(x)-scale, float(y)-scale, float(x)+scale, float(y)+scale)
me = self.iface.mapCanvas()
me.setExtent(rect)
me.refresh()
m.setCenter(QgsPointXY(x, y))
m.setColor(QColor(255, 0, 0))
m.setIconSize(7)
m.setIconType(QgsVertexMarker.ICON_X) # or ICON_CROSS, ICON_X
m.setPenWidth(3)
else:
print('Unchecked going to close socket')
s.close()
A sender computer send the lat/long values & this program marks the lat/long values on map canvas it happens when my toggle button is checked that's good. But when i make it unchecked by calling socket close() method why its not closing the socket (even it prints else part 'Unchecked going to close socket' but s.close() not closing the socket).
Answer
I add an answer here because I already implements something like this in a QGIS plugin but as @user2856 says in comments, this question is better suited for StackOverflow as it is not related to specific GIS problems...
You might get better results by using the toogled signal of your action and put your logic in the callback.
Something like this (in pseudo-code):
def initGui(self):
[...]
self.iconAction.toggled.connect(self.icon_action_handler)
def icon_action_handler(self):
if self.iconAction.isChecked():
self.start_socket_and_show_markers() # a function to start the socket (better to run it in a different thread) and show the marker you receive
else:
self.close_socket() # a function to close the socket
In addition, you probably need to run the socket listener in a different thread (look at Qthread) to avoid QGIS hanging.
No comments:
Post a Comment