Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    [SOLVED] QAction To Toggle QWidget

    General and Desktop
    qt5 slots c++ toggle qstackedwidget
    4
    7
    2233
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      Adept 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!

      1 Reply Last reply Reply Quote 0
      • A
        Adept last edited by Adept

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • A
          alex_malyu 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 );

          A 1 Reply Last reply Reply Quote 0
          • A
            Adept @alex_malyu 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?

            1 Reply Last reply Reply Quote 0
            • S
              Sam last edited by

              @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
                  if(toggled)
                  {
                      container->setCurrentWidget(menuWidget);
                      toggled = false;
                  }
                  else if(toggled == false)
                  {
                      container->setCurrentWidget(mdiContainer);
                      toggled = true;
                  }
              }
              
              A 1 Reply Last reply Reply Quote 0
              • A
                Adept @Sam last edited by

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

                1 Reply Last reply Reply Quote 0
                • Chris Kawa
                  Chris Kawa Moderators last edited by

                  You could also simplify this a little:

                  void MainWindow::menuToggled()
                  {
                       auto widget = (container->currentWidget() == menuWidget) ? mdiContainer : menuWidget;
                       container->setCurrentwidget(widget);
                  }
                  
                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post