Important: Please read the Qt Code of Conduct -

[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);
    connect(menuqaction, SIGNAL(triggered()), this, SLOT(showMenu()));
    void MainWindow::showMenu()

    So the first part (showing the menuWidget) works, but I can't toggle out of it.

  • This post is deleted!

  • 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
    Hey, thanks for helping. I am back to square one with this piece of code:
    Could you please tell me where I'm going wrong here?

  • @Adept

    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
            toggled = false;
        else if(toggled == false)
            toggled = true;

  • @Sam alright, thanks man, I got it fixed now :)

  • Moderators

    You could also simplify this a little:

    void MainWindow::menuToggled()
         auto widget = (container->currentWidget() == menuWidget) ? mdiContainer : menuWidget;