[PyQt] Question about QMenu's triggered event (repost)

Tony Willis twillis449 at gmail.com
Sun May 31 22:36:16 BST 2009


Sorry about the repost but  I think that the PyQt mailing list swallowed 
the text with my
question ...

According to the Qt4 docs, a QMenu is supposed to be capable of emitting a
`triggered' event which contains the action which was triggered. (I realize
that its a better coding practice to connect the `triggered' event 
associated
with individual QActions to their own separate callbacks, but I'm porting an
application from Qt3 to Qt4 and I'd prefer not to have to write a whole
bunch,~50, of new callbacks.) So I'd like to get the more global QMenu
triggered event to work. Unfortunately, with PyQt, I can't seem to see or
catch the QMenu's triggered event. The following PyQt demo script 
illustrates
the problem. Can someone point out what I'm doing wrong?

Thanks

Tony

###############################################
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class mepViewer(QDialog):

    def __init__(self, parent=None):
        super(mepViewer, self).__init__(parent)
        self.setWindowTitle('QAction demo.py')

        # The status bar gives status and progress.
        # (do this one first, so we can send messages)
        self._statusBar = QStatusBar()
        self._statusBar.showMessage('This is the status bar')

        # The menu-bar:
        self._menuBar = QMenuBar()
        self._menuBar.addMenu(self.make_file_menu())


        # Make the overall vertical layout:
        vlayout = QVBoxLayout()
        vlayout.addWidget(self._menuBar)
        self.setLayout(vlayout)

        return None

    def make_file_menu(self):
        """
        Make the file menu on the main menubar.
        """
        actiongroup = QActionGroup(self)
        menu = QMenu('&File', self)
# the next line works
        self.connect(menu, SIGNAL('aboutToShow()'), self.about_to_show)

# the following line doesn't seem to work
        self.connect(menu, SIGNAL('triggered()'), self.was_triggered)

        action = QAction('&New', self)
        action.setToolTip('Open a new .mep file')
        action_id = 1
        action.setData(QVariant(action_id))
        menu.addAction(action)
# the following works just fine
        self.connect(action, SIGNAL('triggered()'), 
self.new_mepTable_dialog)

# unfortunately the following doesn't seem to do anything
        self.connect(menu, SIGNAL('triggered(action)'), self.was_triggered)

# nor does the following
        self.connect(menu, SIGNAL('triggered(QAction)'), self.was_triggered)

# the following works, but doesn't seem to be terribly useful
# as the signal contains a negative integer
        self.connect(menu, SIGNAL('activated(int)'), self.catch_int)


        action = QAction('&Simulated', self)
        action.setToolTip('Simulated .mep table')
        menu.addAction(action)
        self.connect(action, SIGNAL('triggered()'), self.read_mepfile)

        action = QAction('&Exit', self)
        action.setToolTip('Exit command')
        menu.addAction(action)
        self.connect(action, SIGNAL('triggered()'), self.do_exit)

        return menu

    #----------------------------------------------------------------

    def was_triggered(self, action):
        print 'caught triggered event'

    def about_to_show(self):
        print 'in about_to_show'

    def new_mepTable_dialog(self):
        print 'in new_mepTable'

    def read_mepfile (self):
        print 'in read_mepfile'

    def do_exit (self):
        print 'in do_exit'

    def catch_int(self, value):
        print 'in catch_int with value ', value

if __name__ == '__main__':

   app = QApplication(sys.argv)
   mv = mepViewer()
   mv.show()
   app.exec_()













More information about the PyQt mailing list