Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QAction To Toggle QWidget
QtWS25 Last Chance

[SOLVED] QAction To Toggle QWidget

Scheduled Pinned Locked Moved General and Desktop
qt5slotsc++toggleqstackedwidget
7 Posts 4 Posters 2.9k Views
  • 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 Offline
    A Offline
    Adept
    wrote on 21 Apr 2015, 08:13 last edited by Adept
    #1

    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
    0
    • A Offline
      A Offline
      Adept
      wrote on 21 Apr 2015, 11:18 last edited by Adept
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • A Offline
        A Offline
        alex_malyu
        wrote on 22 Apr 2015, 02:26 last edited by alex_malyu
        #3

        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 22 Apr 2015, 02:52
        0
        • A alex_malyu
          22 Apr 2015, 02:26

          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 Offline
          A Offline
          Adept
          wrote on 22 Apr 2015, 02:52 last edited by Adept
          #4

          @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
          0
          • S Offline
            S Offline
            Sam
            wrote on 22 Apr 2015, 06:52 last edited by
            #5

            @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 22 Apr 2015, 06:56
            0
            • S Sam
              22 Apr 2015, 06:52

              @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 Offline
              A Offline
              Adept
              wrote on 22 Apr 2015, 06:56 last edited by
              #6

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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on 22 Apr 2015, 07:15 last edited by
                #7

                You could also simplify this a little:

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

                7/7

                22 Apr 2015, 07:15

                • Login

                • Login or register to search.
                7 out of 7
                • First post
                  7/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved