Checkable for QAction



  • 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


  • Lifetime Qt Champion

    Hi,

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



  • 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


  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

    Aren't you trying to reinvent the QRadioButton ?


Log in to reply
 

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