1

I am creating a system tray application in Mac (El Capitan 10.11.3) using Pyside. However, I am not able to set certain menu entry as disabled, particularly when its a child menu. The setDisabled(True) works for parent menu entry though.

The same code works in Ubuntu/Linux and the child menu entry is disabled.

As the original code is too long to display here, I have made a simple sys tray application that exhibits the same problem.

import sys
from PySide import QtGui

class SystemTrayIcon(QtGui.QSystemTrayIcon):

    def __init__(self, icon, parent=None):
        QtGui.QSystemTrayIcon.__init__(self, icon, parent)
        menu = QtGui.QMenu(parent)
        menu.addAction("Item 1")
        menu.addAction("Item 2")
        disabledItem = menu.addAction("Item 3 Disabled")
        disabledItem.setDisabled(True)
        subMenu = menu.addMenu("Item 4 with sub menu")
        subMenu.addAction("SubMenu 1")
        disabledSubMenu = subMenu.addAction("SubMenu 2 Disabled (Not Working)")
        disabledSubMenu.setDisabled(True)
        disabledSubMenu2 = subMenu.addMenu("SubMenu Disabled")
        disabledSubMenu2.setDisabled(True)
        self.setContextMenu(menu)

def main():
    app = QtGui.QApplication(sys.argv)
    style = app.style()

    w = QtGui.QWidget()
    trayIcon = SystemTrayIcon(QtGui.QIcon(style.standardPixmap(QtGui.QStyle.SP_FileIcon)))

    trayIcon.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

enter image description here

From the above example, Item 3 Disabled (which is a parent menu) is disabled when using setDisabled(True). But SubMenu 2 Disabled is not working, which is a child menu to Item 4 with sub menu.

One thing to note is SubMenu Disabled is working when its set as addMenu item than using addAction.

Any help regarding this is highly appreciated. Thanks!

nbk
  • 1,992
  • 2
  • 19
  • 34

2 Answers2

0

http://doc.qt.io/qt-4.8/qaction.html#setDisabled the name of the method is same with qmenu.But qmenu's set disabled is inherite from qwidget while qaction's method is only for itself and it's not for the user interface. also qaction is abstract what you can see is a qmenu actually.

Shihe Zhang
  • 2,641
  • 5
  • 36
  • 57
  • Thanks for the reply. But one thing to note is, its actually working in other platforms. I have tested it in Ubuntu and child menu is disabled by using setDisabled(true). I am not sure about the behavior in windows. Will test & update the question. – nbk Jul 11 '16 at 05:24
0
#This is the example code for Setting setDisabled(True) for a child .
#If your are not expecting this answer, sorry.
#It is PyQt4, but you can try with PySide with small changes.

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class Window (QtGui.QWidget):
    def __init__(self, parent=None):[![enter image description here][1]][1]      

        super(Window, self).__init__(parent)  

        menubar    = QtGui.QMenuBar (self)
        menubar.setObjectName('menubar')

        menu       = QtGui.QMenu(menubar)
        menu.setObjectName('menu')
        menu.setTitle ('File')
        menubar.addAction (menu.menuAction())    

        menu.addAction ("Item 1")       
        menu.addAction ("Item 2")    

        disabledItem    = menu.addAction("Item 3 Disabled")
        disabledItem.setDisabled(True)

        subMenu         = menu.addMenu("Item 4 with sub menu")
        subMenu.addAction ("SubMenu 1")       

        disabledSubMenu = subMenu.addAction("SubMenu 2 Disabled (Not Working)")
        disabledSubMenu.setDisabled (True)   

        disabledSubMenu2 = subMenu.addMenu("SubMenu Disabled")
        disabledSubMenu2.setDisabled(True)       

        self.resize(513, 203)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())
Subin Gopi
  • 541
  • 2
  • 12
  • I have tried setEnabled(False) & its also not working :(. Thanks for the suggestion though! – nbk Jul 15 '16 at 10:17