2

I created a Python plugin in QGIS which is working fine. Now I want that whenever a user clicks on the plugin button (located on top of QGIS bar) it counts the number of clicks. If the number of clicks is odd my plugin should start running and if the number of clicks is even it should stop running (interrupting its execution). How can I do this and how can I map my code to this button so that it knows the number of clicks?

below is my plugin button


What I've done so far

    def initGui(self):
        icon_path = ':/plugins/my_tracker/icon.png'
        self.iconAction = self.add_action(
        icon_path,
        text=self.tr(u'My tracker plug'),
        callback=self.run,
        parent=self.iface.mainWindow())

    def run(self):
        if (self.iconAction.isChecked()):
            print ('Checked')
            self.show_markers()  // a method to do something

        else:
            print ('Unchecked')
            print("going to close socket", mySocket.close())

The problem is when I click on the plugin button it always runs the else part. It's not running the if part. Why?

10
  • 2
    1. I assume by "my plugin should be start" you mean you wnat to open or close a window or form with commands for your plugin. 2. Unless you really need to count the number of clicks for a specific reason, the approach most widely used is to check if the window is open or closed. If the window is closed, then a click would open it, if it's open, the click would close it. 3. Have a look at the code you already have for the plugin. Do you understand what it does (Each line or function)? That's the first thing you need to do.
    – jberrio
    Jan 25 '19 at 3:44
  • If you understand it, you should able able to identify where you can insert some conditions to allow the open/close window events to happen. When you are at that stage, if a command is not working the way you expect it to, maybe you can ask a few questions here. In the meantime, maybe check a tutorial in how to build and understand the code of your plugin?
    – jberrio
    Jan 25 '19 at 3:45
  • I don't want to open any windows i build plugin which print(using vertex marker) the lat/long value per second on map canvas. suppose i have 1000 of lat long value if i run my plugin it start showing the lat/long position on map canvas but in case i want to stop running my plugin(at any time) before it reach the 1000 value so i just want whenever i click on plugin button it count the no of clicks if it odd it will start (run) my plugin if no of click is even it stop my plugin. Jan 25 '19 at 5:19
  • @jberrio any idea? Jan 25 '19 at 10:57
  • 2
    You haven't set checkable=True as per the answer Joseph and I linked to.
    – user2856
    Jan 27 '19 at 4:04
1

Solution...

def add_action(
    self,
    icon_path,
    text,
    callback,
    checkable=False,
    enabled_flag=True,
    add_to_menu=True,
    add_to_toolbar=True,
    status_tip=None,
    whats_this=None,
    parent=None):

    icon = QIcon(icon_path)
    action = QAction(icon, text, parent)
    action.triggered.connect(callback)
    action.setEnabled(enabled_flag)
    action.setCheckable(checkable)


def initGui(self):   
    icon_path = ':/plugins/my_tracker/icon.png'
    self.iconAction = self.add_action(
        icon_path,
        text=self.tr(u'My tracker plug'),
        callback=self.run,
        checkable=True,
        parent=self.iface.mainWindow())

def run(self):
    if self.iconAction.isChecked():
        print ('Checked')
        self.show_markers()          // a method to do something
    else:
        print('Unchecked going to close socket')
        mySocket.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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