3

In my QGIS plugin, I have a checkable QIcon which changes image depending on its state.

Is it also possible to change its tooltip text (i.e. if icon is checked, the text would read "myPlugin ON"; unchecked would read "myPlugin OFF")?

At the moment, it only reads "myPlugin". This is the code I use:

def add_action(
    self,
    toolbar,
    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)

    if status_tip is not None:
        action.setStatusTip(status_tip)

    if whats_this is not None:
        action.setWhatsThis(whats_this)

    if add_to_toolbar:
        self.toolbar.addAction(action)

    if add_to_menu:
        self.iface.addPluginToMenu(
            self.menu,
            action)

    self.actions.append(action)
    return action

def initGui(self):
    icon = QIcon()
    icon.addFile( ':/plugins/myPlugin/icons/Off.png', state=QIcon.Off )
    icon.addFile( ':/plugins/myPlugin/icons/On.png', state=QIcon.On )
    self.pluginButton = self.add_action(
        self.toolbar,
        icon,
        text=self.tr(u'myPlugin'),
        callback=self.myFunc,
        checkable=True,
        enabled_flag=False,
        parent=self.iface.mainWindow())

I am using QGIS 2.18.2 for Win7 64-bit.

  • this is a theoretical thought: currently, you call the "tr" function. what if you call a custom function which checks which state the checkbox has, and then call "tr" function with the string "plugin on/off" respectively – LaughU Oct 2 '17 at 11:46
  • a more detailed idea: in the function, you could try with icon.state()´ or icon.Mode()` and see if something changes to get the state of the icon and thus change the text. sorry for not writting a more detailed idear but i currently cant test it :( – LaughU Oct 2 '17 at 11:58
  • Your last idea led me to a solution. I have a self.myFunc callback function which checks the state already. I simply had to add self.pluginButton.setText('myPlugin ON') when state is true and the opposite when state is false. Can you post it as an answer please :) – Joseph Oct 2 '17 at 12:07
2

To sum up the results from the comments:

Create a callback function which catches the icon.State() and check its value:

The function could look like this:

myFunc(self):
   if icon.State() == 1: # disabled
      self.pluginButton.setText('myPlugin OFF')
   elif icon.State() == 0: # enabeld
      self.pluginButton.setText('myPlugin On')

Or alternatively:

myFunc(self, checked):
   if checked:
      self.pluginButton.setText('myPlugin ON')
   else:
      self.pluginButton.setText('myPlugin OFF')
  • 1
    @Joseph most welcome! – LaughU Oct 2 '17 at 12:48

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.