Prevent a QMenu from closing when one of its QAction is triggered

前端 未结 6 1870
北荒
北荒 2020-12-15 06:30

I\'m using a QMenu as context menu. This menu is filled with QActions. One of these QActions is checkable, and I\'d like to be able to check/uncheck it without closing the c

6条回答
  •  北海茫月
    2020-12-15 07:17

    Use a QWidgetAction and QCheckBox for a "checkable action" which doesn't cause the menu to close.

    QCheckBox *checkBox = new QCheckBox(menu);
    QWidgetAction *checkableAction = new QWidgetAction(menu);
    checkableAction->setDefaultWidget(checkBox);
    menu->addAction(checkableAction);
    

    In some styles, this won't appear exactly the same as a checkable action. For example, for the Plastique style, the check box needs to be indented a bit.

提交回复
热议问题