【问题标题】:QAction checkable at time in QMenu with nested menu [duplicate]QAction 可在 QMenu 中使用嵌套菜单进行检查 [重复]
【发布时间】:2019-08-12 07:23:38
【问题描述】:

我想创建一个包含三个级别的嵌套菜单 File->Circuit->then 选项为 Full of half,后面的选项可检查和排他。我已经在可检查的项目方面实现了这一点,但不是排他性的。

最初我使用此示例 (One QAction checkable at time in QMenu) 在组中设置独占性,尽管我无法添加另一个嵌套组级别。我可以设置 File -> then Circuit selection 但不能设置全选或半选,所以我选择下面的方法来设置菜单级数。

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_())

【问题讨论】:

  • 我不懂你,你自己解释清楚
  • 我按照链接的示例对上面的代码使用了一个操作组,它按预期工作。您能否发布包含QActionGroup 的代码并说明它为什么不起作用?
  • 希望这更清楚
  • 并非如此。当您尝试问题中链接的示例时,您仍然没有显示出了什么问题。我在链接问题中接受的答案之后发布了一个答案,据我所知(除非我误解了问题)。
  • @anthonycrimin 您的问题完全重复,因为这与另一个问题中的问题相同。如果您尝试过某事但失败了,这不是重复的,但这不是您的情况。

标签: python pyqt pyqt5 qmenu


【解决方案1】:

按照问题中链接的示例中接受的答案:

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()

【讨论】:

  • 谢谢,正是我想要的。
猜你喜欢
  • 2018-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
相关资源
最近更新 更多