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. Checkable for QAction
Qt 6.11 is out! See what's new in the release blog

Checkable for QAction

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 3.2k Views 2 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    QAction * action1 = act = new QAction(QIcon(":/images/my.png"), tr(" View"), this);
    connect(act, SIGNAL(triggered()), d_view, SLOT(showView1()));
    act->setCheckable(true);

    QAction * action2 = act = new QAction(QIcon(":/images/my1.png"), tr(" View"), this);
    connect(act, SIGNAL(triggered()), d_view, SLOT(showView2()));
    act->setCheckable(true);

    it is part of group of Actions

    QActionGroup viewGroup = new QActionGroup(this);
    d_viewGroup->addAction(action1);
    d_viewGroup->addAction(action2);
    action1->setChecked(true);

    When I click on action1 for first time the slot showView1 gets called butwhen I again click on action1 the showView1 does not get called

    What is reason for that . If I want to two actions to be mutually exclusive but the respective slots should be called on every click on the action . how to do that

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

      Hi,

      That's because triggered doesn't get fired when calling setChecked. You should use toggled

      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
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        QAction * action1 = act = new QAction(QIcon(":/images/my.png"), tr(" View"), this);
        connect(act, SIGNAL(toggled(bool)), d_view, SLOT(showView1(bool)));
        act->setCheckable(true);

        QAction * action2 = act = new QAction(QIcon(":/images/my1.png"), tr(" View"), this);
        connect(act, SIGNAL(toggled()), d_view, SLOT(showView2(bool)));
        act->setCheckable(true);

        it is part of group of Actions

        QActionGroup viewGroup = new QActionGroup(this);
        d_viewGroup->addAction(action1);
        d_viewGroup->addAction(action2);
        action1->setChecked(true);

        the toggled is called whenever there is change in state of QAction . In this case event if I connect to toggled(bool) then also

        1. If I click on action1 , then showView2 slot is called two times one with true bool and other with false bool
        2. if I click on action2 , then also showView1slot is called two times one with true bool and other with false bool

        My requirement when I a call any action only that action should be called not other action in the group should be called and also when I click on the action any time the respective slot should be called like a menu item

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

          Because by default QActionGroup is exclusive, call QActionGroup::setExclusive(false)

          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
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #5

            if I QActionGroup::setExclusive(false) and then have

            QAction * action1 = act = new QAction(QIcon(":/images/my.png"), tr(" View"), this);
            connect(act, SIGNAL(toggled(bool)), d_view, SLOT(showView1(bool)));
            act->setCheckable(true);

            QAction * action2 = act = new QAction(QIcon(":/images/my1.png"), tr(" View"), this);
            connect(act, SIGNAL(toggled()), d_view, SLOT(showView2(bool)));
            act->setCheckable(true);

            it is part of group of Actions

            QActionGroup viewGroup = new QActionGroup(this);
            d_viewGroup->addAction(action1);
            d_viewGroup->addAction(action2);
            action1->setChecked(true);

            Will it possible when I click I action1 the only action1 is will excuted and if I click on action2 then only action2 will be executed

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

              Isn't that what's happening when setting exclusive to false ?

              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
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by
                #7

                As I understand if we I

                QActionGroup::setExclusive(true) and then have

                QAction * action1 = act = new QAction(QIcon(":/images/my.png"), tr(" View"), this);
                connect(act, SIGNAL(triggered()), d_view, SLOT(showView1(bool)));
                act->setCheckable(true);

                QAction * action2 = act = new QAction(QIcon(":/images/my1.png"), tr(" View"), this);
                connect(act, SIGNAL(triggered()), d_view, SLOT(showView2(bool)));
                act->setCheckable(true);

                it is part of group of Actions

                QActionGroup viewGroup = new QActionGroup(this);
                d_viewGroup->addAction(action1);
                d_viewGroup->addAction(action2);
                action1->setChecked(true);

                ----------------------it works like group of radiobuttons-------------------

                but if we have a

                I QActionGroup::setExclusive(false) and then have

                QAction * action1 = act = new QAction(QIcon(":/images/my.png"), tr(" View"), this);
                connect(act, SIGNAL(toggled(bool)), d_view, SLOT(showView1(bool)));
                act->setCheckable(true);

                QAction * action2 = act = new QAction(QIcon(":/images/my1.png"), tr(" View"), this);
                connect(act, SIGNAL(toggled()), d_view, SLOT(showView2(bool)));
                act->setCheckable(true);

                it is part of group of Actions

                QActionGroup viewGroup = new QActionGroup(this);
                d_viewGroup->addAction(action1);
                d_viewGroup->addAction(action2);
                action1->setChecked(true);

                then ------------------it is a drop down menu where each action will executed at once when click and when click it as another action the relevant slot will be called

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

                  Aren't you trying to reinvent the QRadioButton ?

                  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

                  • Login

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