[SOLVED] QAction To Toggle QWidget
-
wrote on 21 Apr 2015, 08:13 last edited by Adept
Hello again,
I would like to know how to toggle a widget (click once - menuWidget shown, click again - disappeared) in QStackedWidget.Here is what I have so far:
QAction *menu = new QAction("Menu", this);
AND
connect(menuqaction, SIGNAL(triggered()), this, SLOT(showMenu()));
AND
void MainWindow::showMenu()
{
stackedWidget->setCurrentWidget(menuWidget);
}
So the first part (showing the menuWidget) works, but I can't toggle out of it.
Thanks! -
wrote on 21 Apr 2015, 11:18 last edited by AdeptThis post is deleted!
-
wrote on 22 Apr 2015, 02:26 last edited by alex_malyu
Below comment is based on name of the variable, can't say for sure due to lack of source code.
Your problem is mostly likely related to 'map'Keep in the mind that slot is a special function in the QObject subclass.
But regular function is not a slot.I would recommend to always check
value returned by connect:bool ok = connect(showMenu, SIGNAL(triggered()), map, SLOT(map()));
Q_ASSERT( ok ); -
Below comment is based on name of the variable, can't say for sure due to lack of source code.
Your problem is mostly likely related to 'map'Keep in the mind that slot is a special function in the QObject subclass.
But regular function is not a slot.I would recommend to always check
value returned by connect:bool ok = connect(showMenu, SIGNAL(triggered()), map, SLOT(map()));
Q_ASSERT( ok );wrote on 22 Apr 2015, 02:52 last edited by Adept@alex_malyu
@macgab
@mcosta
Hey, thanks for helping. I am back to square one with this piece of code:
http://pastebin.com/CcRStL0B
Could you please tell me where I'm going wrong here? -
wrote on 22 Apr 2015, 06:52 last edited by
You are declaring the toggle variable within your slot, whenever menuToggled() is called it will always initialize it to true, instead you should move the declaration of this variable to the headerFile.
void MainWindow::menuToggled() { bool toggled = true; // THIS IS WRONG !!!!! toggle WILL ALWAYS BE true if(toggled) { container->setCurrentWidget(menuWidget); toggled = false; } else if(toggled == false) { container->setCurrentWidget(mdiContainer); toggled = true; } }
-
You are declaring the toggle variable within your slot, whenever menuToggled() is called it will always initialize it to true, instead you should move the declaration of this variable to the headerFile.
void MainWindow::menuToggled() { bool toggled = true; // THIS IS WRONG !!!!! toggle WILL ALWAYS BE true if(toggled) { container->setCurrentWidget(menuWidget); toggled = false; } else if(toggled == false) { container->setCurrentWidget(mdiContainer); toggled = true; } }
-
You could also simplify this a little:
void MainWindow::menuToggled() { auto widget = (container->currentWidget() == menuWidget) ? mdiContainer : menuWidget; container->setCurrentwidget(widget); }
1/7