0

I want to create a nested menu with three levels File->Circuit->then with an option of Full of half with the later options checkable and exclusive. I have achieved this with respect to the items being checkable though not exclusive.

Initially I used this example (One QAction checkable at time in QMenu) to setExclusive in a group though I could not add another nested group level. I can set the File -> then Circuit selection but not the full or half selection, so is the reason I chose the method below to set the number of menu levels.

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, QMenu, QApplication

class Example(QMainWindow):

    def __init__(self):
        super().__init__()

        self.menubar = self.menuBar()
        self.fileMenu = self.menubar.addMenu('File')

        self.impMenu = QMenu('Circuit', self)
        self.impAct0 = QAction('Half Bridge', self, checkable=True, checked=True) 
        self.impAct1 = QAction('Full Bridge', self, checkable=True, checked=False) 
        self.impMenu.addAction(self.impAct0)
        self.impMenu.addAction(self.impAct1)
        self.fileMenu.addMenu(self.impMenu)

        self.setGeometry(300, 300, 300, 200)
        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
  • I do not understand you, explain yourself better – eyllanesc Aug 12 '19 at 11:18
  • I followed the linked example using an action group for the code above and it works as expected. Could you post your code with the `QActionGroup` included and indicate why it doesn't work? – Heike Aug 12 '19 at 12:08
  • hopefully this is clearer – anthony crimin Aug 12 '19 at 12:56
  • Not really. You still didn't show what went wrong when you tried the example linked in the question. I posted an answer below following the accepted answer in the linked question which works correctly as far as I can tell (unless I misunderstood the question). – Heike Aug 12 '19 at 14:42
  • @anthonycrimin Your question is an exact duplicate since that is the same as asked in the other question. It was not a duplicate if you had tried something and failed, but it is not your case. – eyllanesc Aug 12 '19 at 14:45

1 Answers1

0

Following the accepted answer in the example linked in the question:

class Example(QMainWindow):

    def __init__(self):
        super().__init__()

        self.menubar = self.menuBar()
        self.fileMenu = self.menubar.addMenu('File')

        self.impMenu = QMenu('Circuit', self)
        self.impAct0 = QAction('Half Bridge', self, checkable=True, checked=True)
        self.impAct1 = QAction('Full Bridge', self, checkable=True, checked=False)
        self.impMenu.addAction(self.impAct0)
        self.impMenu.addAction(self.impAct1)
        self.fileMenu.addMenu(self.impMenu)

        self.setGeometry(300, 300, 300, 200)

        self.action_group = QActionGroup(self)
        self.action_group.addAction(self.impAct0)
        self.action_group.addAction(self.impAct1)
        self.action_group.setExclusive(True)

        self.show()

Heike
  • 24,102
  • 2
  • 31
  • 45