0

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

closed as off-topic by user2856, Vince, LaughU, Erik, Jochen Schwarze Feb 11 at 10:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions relating to general IT or with no clear GIS component, are off-topic here but can be researched/asked at Stack Overflow (software development), Super User (computing hardware and software), Database Administrators (relational databases) and other SE sites" – user2856, Vince, LaughU, Erik, Jochen Schwarze
If this question can be reworded to fit the rules in the help center, please edit the question.

3

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.

  • I don't know about Qthread i am beginner in python can you tell me exactly what should i add in my code to work properly like earlier i mentioned in comments. – Rahul Verma Feb 10 at 14:01
  • 1
    this is a good start point with examples to work with Qthread in QGIS plugin.snorfalorpagus.net/blog/2013/12/07/… – YoLecomte Feb 10 at 18:59
  • Ok please accept the answer if it help to solve your problem. – YoLecomte Feb 11 at 13:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.