views:

157

answers:

3

I create this object when I want to create a QAction. I then add this QAction to a menu:

class ActionObject(object):
  def __init__(self, owner, command):
    action = QtGui.QAction(command.name, owner)
    self.action = action
    self.command = command
    action.setShortcut(command.shortcut)
    action.setStatusTip(command.name)
    QtCore.QObject.connect(action, QtCore.SIGNAL('triggered()'), self.triggered)
  def triggered(self):
    print("got triggered " + self.command.id + " " + repr(checked))

Unfortunately, when the menu item is selected, the 'triggered' function is not called. QtCore.QObject.connect() returns True. Nothing is printed on the console to indicate that anything is wrong, and no exception is thrown.

How can I debug this? (or, what am I doing wrong?)

A: 

I don't see you adding action to any menu in this code (indeed I see no addAction call anywhere), and there's a peculiar use of a variable checked that is never defined anywhere in your triggered method (is it a global defined elsewhere?). Both of these issues suggest you have other code that you're not showing (the code that adds this action to some menu or toolbar and defines the global variable checked -- or did you omit a parameter in the def triggered statement, perhaps...?), and that would be where the bugs lie (this code is a subset of a potentially correct program... but clearly there are missing parts, and how do we know they are correct?-).

Alex Martelli
That's not an answer to "how do I debug signals in PyQt4," really...Yes, the addAction comes from the return value of a function that creates this object. For more code, seehttp://pastebin.com/SuKvBLVmThe "checked" variable only matters by the time the slot is called, which doesn't happen, so it's beside the point. (If you want to know, it's a hold-over from when I tried to register the triggered function with the (bool) signature, as per the optional argument in the documentation -- that also didn't work)So, now that you have all of this information... how do I debug this?
Jon Watte
More information: Connecting to global functions works, it's only connecting to member functions (bound or not) that doesn't work.
Jon Watte
A: 

Looks like your debugging has to occur in one of the two classes you don't provide; where you're attaching attributes to them then passing them to ActionObject as parameters.

I've created an example without this, as I have no idea what your other two classes look like. The third, parent class, I don't need, because of course it can be any generic Class that has inherited QWidget/QMainWindow/QDialog, etc

class ActionObject(object):
  def __init__(self, owner=None, command=None):
    self.menuBar = QtGui.QMenuBar(self)
    self.setMenuBar(self.menuBar)
    # Make sure the menu item's parent is the menu
    self.menuGeneric = QtGui.QMenu(self.menuBar)
    self.menuGeneric.setTitle('&Generic')
    # Create and add the action in one line
    self.menuBar.addAction(self.menuGeneric.menuAction())
    QtCore.QObject.connect(self.menuGeneric, qc.SIGNAL('triggered()'), self.triggered)
  def triggered(self):
    print "got triggered"
mleep
A: 

Did you see the pastebin that provides the code? pastebin.com/SuKvBLVm

Anyway, thanks for writing some code, but it still doesn't answer the question: HOW DO I DEBUG THIS PROBLEM!!!?!?!? Honestly, I don't want fish. I want a fishing lesson.

Jon Watte
This is not an answer.
Mark Byers