[PYSIDE] Get a Buttons connected function

I’m Sub-classing a QToolButton and I’m wondering if it’s possible query the function the tool is connected to.

My end goal is to kind of re-create a Right-Click Menu on a button with an “Edit cmd” and “Open cmd” similar to Maya’s shelf button. I have some functions to get the script and line numbers but it’s a matter of inspecting the correct function and not the menu function. That way when I Right-Click on “Open cmd” it will launch a say sublime or Notepad++ to the line number of the function the button is connected to. Possible?

Cheers,
-Sean

I assume you are referring to the Action that is connected to the ToolButton?

Well, not the Action of the menu itself but the connected command of the button.

So if I would do a Right-Click menu with Edit Cmd it would grab the buttons function.

Grab my_command and tell me the file location and line number

self.someButton.clicked.connect(self.my_command)

line #45 in myTool.py

Yes, if I inspect the Action of the menu then it just tells me the function of the menu which works fine but I’m after the button itself. I guess trying to inspect a bit deeper.
Hope I’m making sense.

Quick Update:
In my Right-Click Menu in my Button Class I have I did a quick print of my object:
print self.dict
In that dict is a clicked Signal Instance
{‘fontBold’: True, ‘clicked’, <PySide.QtCore.SignalInstance object at 0x000000004B603E10>, …}
Might be able to get a that.

Man, that dict has a ton of object info.

I would probably create a custom “command” class to hold the function and a custom button class that consumes a “command” class. then the other aspects of the button can query the “command” class.

Hmm, that could do it.
Thanks!

That worked like a charm. I’ll share what I got.
So instead of doing a normal clicked.connect I created a Connect class.

Now when I Right-Click on a button I get a menu of choice of editor to open to that line number.
Super Awesome…at least I think so :wink:

Before:

self.button.clicked.connect(self.my_command)

After:

Connect(self.buttonObject, self.my_command)

import inspect
import subprocess
from distutils.spawn import find_executable

class RightClickMenu(QtGui.QMenu):
    def __init__(self, *args, **kwargs):
        super(RightClickMenu, self).__init__(*args)

        # Prepare the parent widget for using the right-click menu
        self.parentWidget().setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.parentWidget().customContextMenuRequested.connect(self.showMenu)

    def showMenu(self, *args):
        self.exec_(QtGui.QCursor.pos())


class Connect(object):
    def __init__(self, obj, command, *args, **kwargs):
        super(Connect, self).__init__(*args)

        self.command = command
        self.obj = obj

        # Setup the signal
        self.obj.clicked.connect(self.command)

        menu = RightClickMenu(self.obj)
        menu.addAction('Edit cmd (notepad++)', lambda *args: self.editCommand("notepad"))
        menu.addAction('Edit cmd (sublime)', lambda *args: self.editCommand("sublime"))
        menu.addAction('Print cmd', self.printCommand)
        self.fileLocation = inspect.getsourcefile(self.command)
        self.lineNumber = inspect.getsourcelines(self.command)[1]
        self.sourceCode = inspect.getsourcelines(self.command)[0]

    def editCommand(self, editor):
        """ Open the file using one of the text editors """
        sublime = find_executable("C:\\Program Files\\Sublime Text 2\\sublime_text.exe")
        notepad = find_executable("C:\\Program Files (x86)\\Notepad++\
otepad++.exe")

        if editor == "notepad":
            if notepad:
                subprocess.Popen([notepad, self.fileLocation, "-n{}".format(self.lineNumber)])
        if editor == "sublime":
            if sublime:
                subprocess.Popen([sublime, self.fileLocation + ":{}".format(self.lineNumber)])

    def printCommand(self):
        """ output function """
        def chomp(s):
            if s.endswith('
'):
                return s[:-1]
            else:
                return s

        for code in self.sourceCode:
            code = chomp(code)
            print code

Nice! thanks for sharing!

No, thank you for the suggestion!
Cheers!

[QUOTE=snolan;28873]No, thank you for the suggestion!
Cheers![/QUOTE]

what a great class to have. Thank you, it shall be used wisely