Advertisement
Guest User

QAction not triggering a signal/slot not getting called in PyQt4

a guest
Apr 20th, 2010
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1.  
  2. ActionObject used to hold on to the callback for a QAction:
  3.  
  4.  
  5. class ActionObject(object):
  6.   def __init__(self, owner, command):
  7.     action = QtGui.QAction(command.name, owner)
  8.     self.action = action
  9.     self.command = command
  10.     action.setShortcut(command.shortcut)
  11.     action.setStatusTip(command.name)
  12.     QtCore.QObject.connect(action, QtCore.SIGNAL('triggered()'), self.triggered)
  13.   def triggered(self):
  14.     print("got triggered " + self.command.id + " " + repr(checked))
  15.  
  16.  
  17. Command object used to hold on to miscellaneous command information, and manufacture actions:
  18.  
  19.  
  20. class Command(object):
  21.   def __init__(self, menu, name, prio, id, shortcut=None, icon=None):
  22.     self.menu = menu
  23.     self.name = name
  24.     self.prio = prio
  25.     self.id = id
  26.     self.shortcut = shortcut
  27.     self.icon = icon
  28.   def makeAction(self, owner):
  29.     ao = ActionObject(owner, self)
  30.     return ao.action
  31.  
  32.  
  33. Function that populates a menu bar given a list of command objects:
  34.  
  35.  
  36. menubar = None
  37.  
  38. def build(frame, commands):
  39.   global menubar
  40.   menubar = frame.menuBar()
  41.   for i in ['File', 'Edit', 'Project', 'View', 'Other']:
  42.     menu = menubar.addMenu(i)
  43.     last = 0
  44.     for j in commands:
  45.       if j.menu == i:
  46.         if int(j.prio) > last+1:
  47.           menu.addSeparator()
  48.         last = int(j.prio)
  49.         menu.addAction(j.makeAction(frame))
  50.  
  51.  
  52. "frame" is a QMainWindow, and "commands" is a list of Command objects.
  53.  
  54. Yes, I know that the menus get created, what with them actually showing up in the window and whatnot...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement