简体   繁体   中英

Cannot add actions to OS X Preferences menu

In trying to add action items to the default OS X menu, I'm following the documentation . Below is a short script that, when run, produces:

QMenu: No OSMenuRef created for popup menu

My (apparently inadequate) searches have not revealed any real clues as to what next, or if this is even possible to do.

Script:

from PyQt4.QtGui import QApplication
app = QApplication([])
from PyQt4 import QtGui
menu = QtGui.QMenuBar()
action = QtGui.QAction('Apref', app)
action.setMenuRole(QtGui.QAction.PreferencesRole)
menu.addAction(action)

Details:

  • OS X 10.10
  • Python 2.7
  • Qt version: '4.8.7'
  • SIP version:'4.16.9'
  • PyQt version: '4.11.4'

Simple Answer: The misconception that I had is that when things are sucked into the standard OS X "Preferences", they would all be attached to a menu under "Preferences". The reality is that only ONE action gets associated with Preferences and it is up to the developer to build the system desired. I figured this out by looking at what "Preferences" does with other applications: They always create some app-specific window.

So, without ado, here is a minimum script that implements Preferences. I have no doubt there is a more standard way of handling, but wanted to get this out there.

from PyQt4 import QtCore,QtGui
app = QtGui.QApplication([])

def pmake(*args, **kwargs):
    menu = QtGui.QMenu()
    menu.addAction('This is an action')
    result = menu.exec_(QtCore.QPoint(10,10))
    print 'result="{}"'.format(result)

pact = QtGui.QAction('Preferences', app)
pact.triggered.connect(pmake)
pact.setMenuRole(QtGui.QAction.PreferencesRole)
pmenu = QtGui.QMenu('Preferences')
pmenu.addAction(pact)
menu = QtGui.QMenuBar()
menu.addMenu(pmenu)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM