PDA

View Full Version : Is there a simpler way to send the same signal from many objects?



learning_qt
1st December 2008, 11:53
Hello,

I plan to use menus in my applications. There are many menus and sub menus. Only one sub menu needs one specified status. When I click other menus or sub menus I hope to change the status to normal one. But I do not hope to connect all of the menus or sub menus to the triggered signal.

Is there some smarter ways to resolve this problem? I mean just one line.

just like this:

if (x is triggered)
status changs
else
status changes back

drhex
1st December 2008, 12:39
Could you explain more clearly what you mean by "status"? Does it refer to the presence of a checkbox next to the menu item?

If you have several menu item that are to do similar things, just with a different parameter, check out QSignalMapper.

learning_qt
2nd December 2008, 06:36
Thanks for you reply.

My question is like this:

There Menu1 that consists of sub menuA , sub menuB and so on
Menu2 that consists of sub menu AA, sub menu BB and so on
Menu3 that consists of sub menu AAA, sub menu BBB and so on
... ....

When I click the sub menu AA, The value of status becomes true. But when I click any other menu except sub menu AA, I hope the value of status will become false. I do not hope to connect each sub menu with the signal because there are too many sub menus.
connect(menuAction, SIGNAL(triggered()), this, SLOT(valueChanged));

My question is: Is there some way we can do it in a simpler way?

spirit
2nd December 2008, 06:50
did you see this signal QMenu::triggered (http://doc.trolltech.com/4.4/qmenu.html#triggered).
an example


...
QMenu *menu = menuBar()->addMenu("Test");
QAction *action = new QAction("1", this);
menu->addAction(action);
action = new QAction("2", this);
menu->addAction(action);
action = new QAction("3", this);
menu->addAction(action);
action = new QAction("4", this);
menu->addAction(action);
action = new QAction("5", this);
menu->addAction(action);

connect(menu, SIGNAL(triggered(QAction *)), SLOT(processAction(QAction *)));
...

pdolbey
2nd December 2008, 16:44
Have you looked at QActionGroup - I think you make a group of QActions mutually exclusive.

Pete