Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mac menu bar item doesn't work #18

Closed
sakelariev opened this issue May 5, 2018 · 3 comments
Closed

Mac menu bar item doesn't work #18

sakelariev opened this issue May 5, 2018 · 3 comments

Comments

@sakelariev
Copy link

I'm trying to replicate this System tray & Mac menu bar example and I'm trying to figure out why I can't get the menu to work. I can see the tray icon, but I can't click on it. Short 10-sec video of the result.

Is there something I'm missing for initializing the tray menu?


from fbs_runtime.application_context import ApplicationContext, \ cached_property
import sys

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class AppContext(ApplicationContext):
    def run(self):
        self.main_window.show()
        self.tray_icon.show()
        self.app.setQuitOnLastWindowClosed(False)
        return self.app.exec_()

    @cached_property
    def icon(self):
        return QIcon(self.get_resource('ice-cream-chocolate.png'))
    @cached_property
    def main_window(self):
        result = QMainWindow()
        result.setWindowTitle('Hello Test!')
        result.resize(250, 150)
        return result
    @cached_property
    def menuTray(self):
        menu = QMenu()
        action = QAction("A menu item")
        action2 = QAction("Menu item 2")
        menu.addAction(action)
        menu.addAction(action2)
        return menu
    @cached_property
    def tray_icon(self):
        tray = QSystemTrayIcon()
        tray.setIcon(self.icon)
        tray.setVisible(True)
        tray.setContextMenu(self.menuTray)
        return tray

@mherrmann
Copy link
Owner

Just took me 45 min to figure this out myself. Here's the cause of the problem: The menuTray method in your example creates QAction objects. The problem is that once the method exits, the Python interpreter believes that these objects are no longer needed (because they are not referenced by any other Python code; They're only referenced by Qt). One solution is to save the actions in @cached_propertys:

@cached_property
def menuTray(self):
    menu = QMenu()
    menu.addAction(self.action)
    menu.addAction(self.action2)
    return menu
@cached_property
def action(self):
    return QAction("A menu item")
@cached_property
def action2(self):
    return QAction("Menu item 2")

An alternative solution might be a function called sip.transferTo.

@sakelariev
Copy link
Author

Thanks @mherrmann ! The first solution worked.

@mherrmann
Copy link
Owner

Glad I could help :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants