[PyKDE] KStdAction and friends

Jim Bublitz jbublitz at nwinternet.com
Wed Feb 13 20:34:33 GMT 2002


There was a post to this list re: KStdAction a few days ago.
Although the post provided a workable solution to using KStdAction
under PyKDE2, there's actually an easier way (that is, if I fix
the KStdAction code in PyKDE2, which I just did).

In the pykless.py example (in PyKDE2/examples), the File | Open
menu entry is setup with a standard accelerator as follows (I've
snipped all of the other related code):

self.a = KAccel(self)
self.a.connectItem(KStdAccel.Open, self.slotLoadFile)
...

id = p.insertItem(i18n("&Open"), self.slotLoadFile)
self.a.changeMenuAccel(p, id, KStdAccel.Open)
...

self.toolBar().insertButton(BarIcon("fileopen.xpm"),TOOLBAR_OPEN,
      TRUE ,i18n("Open File"))
self.connect(self.toolBar(), SIGNAL("clicked(int)"),
      self.slotToolbarClicked)
...

def slotToolbarClicked(self, item):
      if item == TOOLBAR_OPEN:
            self.slotLoadFile()


Using KStdAction, you can accomplish the identical result by
deleting ALL the lines above and replacing them with:

openAction = KStdAction.action (KStdAction.Open, self.slotLoadFile)
openAction.plug (p)                    # adds it menu 'p'
openAction.plug (self.toolBar ())      # adds it to the toolbar

alternatively, you can replace the first line with:

openAction = KStdAction.open (self.slotLoadFile)

Either way takes care of the menu item title ('Open'), the standard
accelerator (CTRL-O) and the toolbar icon (fileopen.xpm), but still
allows you to link the File | Open/toolbar action with your own code
(self.slotLoadFile). You don't need to instantiate KStdAction,
because all of its methods are static. 'openAction' is a object of
type KAction. For things that aren't standard actions, you can
create your own actions using KAction. There are other advantages
as well (like being able to XML'ize your GUI)

Notice that the C++ specification for KStdAction::action is:

static KAction * action (StdAction act_enum,
                         const QObject *recvr = 0, 
                         const char *slot = 0, 
                         QObject *parent = 0, 
                         const char *name = 0L) 

The Python call uses 'self.slotLoadFile' to replace BOTH the recvr
and slot parameters shown in the C++ call (the insertItem call
shown in the original code above is from PyQt and works the same
way, replacing the recvr and slot parameters with the name of the
Python method to connect to).

What makes this possible is some magic in sip which I hadn't coded
into the PyKDE2 sip files for KStdAction (and KAction, and probably
some other places as well). This will all be fixed in the upcoming
(a few days) alpha 6 release of PyKDE2. The KStdAction stuff above
won't work with previously released versions of PyKDE2.

For more info on KStdAction and KAction, see the KDE class
reference docs - they have some easy to follow examples (in C++,
but they're easy to follow anyway for non-C++ programmers).

Jim





More information about the PyQt mailing list