[SOLVED] QAction To Toggle QWidget
-
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! -
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 ); -
@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? -
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); }