PDA

View Full Version : mapping menu item with QPushButton



navi1084
26th February 2009, 02:41
Hi,
Can any one please tell, is it possible to map menu item with a normal button in the main window. For example:
Menu has a checkable sub item called EDIT and there is a QPushButton called EDIT in mainwindow . This both should synchronise i.e when i click subitem EDIT, then mainwindow EDIT should turn to checked state.

Can any one help me out????

spirit
26th February 2009, 06:19
is this what you what?


...
QAction *action = new QAction(tr("Action"), this);
action->setCheckable(true);

QMenu *menu = menuBar()->addMenu(tr("File"));
menu->addAction(action);

QPushButton *pb = new QPushButton(tr("Button"), this);
pb->setCheckable(true);

connect(action, SIGNAL(toggled(bool)), pb, SLOT(setChecked(bool)));
connect(pb, SIGNAL(toggled(bool)), action, SLOT(setChecked(bool)));
...

mattie
15th October 2010, 21:56
but shouldn't this happen automatically when you would also add the action to the button? like:

pb->addAction(action);
it seems unintuitive to me that I would have to manually connect signal/slot if that would exactly seem like an advantage of using an action in multiple places that you don't have to connect it each time..?

in any case, I tried it by adding the action to the toggle button, but it doesn't work. So that's why I started searching the net and ended up here :) I just would like confirmation from a qt guru that manually connecting the signal/slot is the right thing to do..