Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can you think of a better code to uncheck an action of a QActionGroup?
Forum Updated to NodeBB v4.3 + New Features

Can you think of a better code to uncheck an action of a QActionGroup?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    So i have a qactiongroup on my QSystemTrayIcon, and every action on it is checkable
    So it kinda works like the widget radiobutton

    the difference is that when you reclick one checked action i want it to be unchecked..
    The only way i tought to accomplish this is :

    @if(!action1->isChecked())
    {
    action1->setCheckable(true);
    action1->setChecked(true);
    }
    else
    action1->setCheckable(false);@

    Simply because clicking on the action always makes it checked...
    U got any better ideas?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      I'm not sure to understand you right, you wan't an exclusive QActionGroup that is not really exclusive ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        It's a bit old topic but I recently had a similar need. An editor that has a three actions: translate, rotate and scale. I needed a way to check exactly one or none of these, i.e. if the current action was clicked it should be unchecked.

        The way I did it was this:

        void makeGroupOptional(QActionGroup* group)
        {
            group->connect(group, &QActionGroup::triggered, [](QAction* action) {
                static QAction* lastAction = nullptr;
                if(action == lastAction) {
                    action->setChecked(false);
                    lastAction = nullptr;
                }
                else
                    lastAction = action;
            });
        }
        

        The nice thing about it is that it's a sort of "fire and forget" function ie. you don't have to worry about the actual contents of the group. You can add, modify and remove actions from the group without ever modifying this piece.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved