February 20, 2011

julio jerez julio jerez
Lab Rat
54 posts

How to set the initial state of a menu action

 

I see that the infeace fo a ction let you do this

  1.    void setCheckable(bool);
  2.     bool isCheckable() const;
  3.  
  4.     bool isChecked() const;

however some time a menu initial state must be checked, or sometime you set it condionally base on xml config file
but I can not see how to do it a creation time:
something like this:

  1. QAction* action = new QAction(this);
  2. action->setText(QApplication::translate("newtonMain", "Autosleep dissabled", 0, QApplication::UnicodeUTF8));
  3. action->setCheckable(true);
  4. // here I'd like to force this action on condiotinally
  5. if (m_autosleep) {
  6.         action-> .....
  7. }
  8.  
  9. subMenu->addAction(action);
  10. connect (action, SIGNAL (triggered(bool)), this, SLOT (OnAutoSleep()));

3 replies

February 20, 2011

xsacha xsacha
Lab Rat
517 posts

Huh? I don’t think I understand this exactly.

You want to set whether it is checked based on the condition of m_autosleep?
Don’t you just use setChecked() [doc.qt.nokia.com] ?

  1. QAction* action = new QAction(this);
  2. action->setText(QApplication::translate("newtonMain", "Autosleep dissabled", 0, QApplication::UnicodeUTF8));
  3. action->setCheckable(true);
  4. action->setChecked(m_autosleep);
  5.  
  6. subMenu->addAction(action);
  7. connect (action, SIGNAL (triggered(bool)), this, SLOT (OnAutoSleep()));

 Signature 

- Sacha

February 20, 2011

julio jerez julio jerez
Lab Rat
54 posts

Oh thank you

  1. action->setChecked(m_autosleep);

is what I was looking for, for some reason I could not find it in the header file.
and yes you are right, I can just call it without testing the condition.

Thank you.

February 20, 2011

Volker Volker
Ant Farmer
5428 posts
julio jerez wrote:
for some reason I could not find it in the header file.

It’s almost always a better idea to look at the fine API docs using Qt Assistant or the online docs [doc.qt.nokia.com]. The header files do not contain the descriptions.

 
  ‹‹ [SOLVED]how can i manipulate big files?      [Solved] Events between loose coupled objects ››

You must log in to post a reply. Not a member yet? Register here!