How do I change the style of checkboxes for checkable QActions in a QMenu?



  • How do I change the style of checkboxes for checkable QActions in a QMenu?

    My menu works as expected but the checks are checkmarks with no boxes. I want to see empty box and checked box.

    My menu has this:
    alt text

    But I want this:
    alt text

    QMenu* mySubmenu = addMenu(tr("MySubmenu"));
    QAction *pAction;
     
    pAction = new QAction("Option 1");
    pAction->setCheckable(true);
    pAction->setChecked(isChecked());
    connect(pAction, &QAction::triggered, this, [=](bool checked) { onSubmenuClick(); } );
    mySubmenu->addAction(pAction);
     
    pAction = new QAction("Option 2");
    pAction->setCheckable(true);
    pAction->setChecked(isChecked());
    connect(pAction, &QAction::triggered, this, [=](bool checked) { onSubmenuClick(); } );
    mySubmenu->addAction(pAction);
    


  • @Qtim hi,friend,welcome.

    First tell you some Qt key words of Qt Style Sheets and Qt Style Sheets Reference,you can search them in Qt help manual. maybe will help you in the some days;

    From the Qt key words Qt Style Sheets Reference, you will find below description:

    List of Sub-Controls

    ::item 
    An item of a QAbstractItemView, a QMenuBar, a QMenu, or a QStatusBar.
    

    List of Pseudo-States

    :checked 
    The item is checked. For example, the checked state of QAbstractButton.
    :unchecked 
    The item is unchecked.
    

    I find one snippet from internet, maybe help you.

    /*Qmenu Style Sheets*/
    
     QMenu {
     background-color: white; /* sets background of the menu :white*/
     border: 1px solid white;
     }
    
     QMenu::item {
     /* sets background of menu item. set this to something non-transparent
     if you want menu color and menu item color to be different */
     background-color: transparent;
     padding:8px 32px;
     margin:0px 8px;
     border-bottom:1px solid #DBDBDB;
     }
    
     QMenu::item:selected { /* when user selects item using mouse or keyboard */
     background-color: #2dabf9;
     background-image: url(:/images/hydro.png)
     }
    
     /** Note: below QSS */
     QMenu::item:checked {
     background-image: url(:/images/checked.png) /** Note: Here to set item checed */
     }
    
     QMenu::item:unchecked {
     background-image: url(:/images/unchecked.png) /** Note: Here to set item uncheced */
     }
    

    You will know how to set styles when you read Qt Style Sheets in Qt help manual.

    Reference

    jianshu.com


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.