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. Collapsable menu bar moves label.
Forum Updated to NodeBB v4.3 + New Features

Collapsable menu bar moves label.

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 948 Views 1 Watching
  • 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.
  • R Offline
    R Offline
    Radio1985
    wrote on last edited by
    #1

    Hi All,
    I have a simple desktop widget application creating using QT Creator.
    It has a left menu bar with with some buttons and labels. The menu button has an icon as shown below.
    4fd50a71-c61e-4b30-a968-f39b2b274716-image.png

    I want to collapse the left menu bar (i.e blue color frame) to the left when I press the menu button. The way I have implemented, I see that "Menu" label moves to the left and goes on top of the icon as shown below.
    366dca98-b590-4edb-a5cc-fc8ec1487e4e-image.png

    As shown above the "Menu" label goes on top of the button icon after collapse.
    Any idea how I can fix this?

    I am using the following code.
    In mainwindow.cpp: when the menu button clicked:

    void MainWindow::on_menuIcon_clicked()
    {
        // Calculate the target width for the sidebar
        int targetWidth = isSidebarCollapsed ? sidebarExpandedWidth : sidebarCollapsedWidth;
    
        // Create an animation to change the width
        QPropertyAnimation *animation = new QPropertyAnimation(ui->leftMenu, "maximumWidth", this);
        animation->setDuration(animationDuration);
        animation->setStartValue(ui->leftMenu->maximumWidth());
        animation->setEndValue(targetWidth);
        animation->start();
    
        // Update the collapsed state
        isSidebarCollapsed = !isSidebarCollapsed;
    }
    
    

    In MainWindow.h:

    bool isSidebarCollapsed; // Define the variable here
    
    // Constants for sidebar width and animation duration
    const int sidebarExpandedWidth = 198;  // Adjust this to your needs
    const int sidebarCollapsedWidth = 60;  // Adjust this to your needs
    const int animationDuration = 500;      // Adjust this to your needs
    
    JonBJ jsulmJ 2 Replies Last reply
    0
    • R Radio1985

      @Axel-Spoerl
      I removed the label and added a text on the push button as shown below.
      579ed9ed-4285-4472-8927-3fbcb944f181-image.png

      But when my frame shrinks, the button shrinks and the icon also moves to the left.

      ef88bae4-2aa1-44b9-bfbe-e2de5ceeed81-image.png

      The behavior I want is the button shrinks until I see only the button icon and I don't see any text labels, something like below.
      9a527020-bda7-4eba-a447-dd4d71bee869-image.png

      Any ideas? If you think that this behavior can be achieved in a different way please recommend it or share any resources.

      Thanks!

      Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #8

      @Radio1985
      Hi,

      The short answer is probably:
      pushButton->setStyleSheet("text-align:left;");

      But I am not so sure if I understand the use case correctly. There is a menu, created manually with buttons. On top of that, there is a button, called menuIcon(confuses me: why not menuButton?), associated with the text "Menu". So what is it supposed to do, when the menu is already there anyway? I would expect the button to expand the menu.
      For this purpose, I'd rather use a QMenu, like shown in this example.
      Or a tool button. Or assign a menu to a push button.

      Software Engineer
      The Qt Company, Oslo

      R 1 Reply Last reply
      1
      • R Radio1985

        Hi All,
        I have a simple desktop widget application creating using QT Creator.
        It has a left menu bar with with some buttons and labels. The menu button has an icon as shown below.
        4fd50a71-c61e-4b30-a968-f39b2b274716-image.png

        I want to collapse the left menu bar (i.e blue color frame) to the left when I press the menu button. The way I have implemented, I see that "Menu" label moves to the left and goes on top of the icon as shown below.
        366dca98-b590-4edb-a5cc-fc8ec1487e4e-image.png

        As shown above the "Menu" label goes on top of the button icon after collapse.
        Any idea how I can fix this?

        I am using the following code.
        In mainwindow.cpp: when the menu button clicked:

        void MainWindow::on_menuIcon_clicked()
        {
            // Calculate the target width for the sidebar
            int targetWidth = isSidebarCollapsed ? sidebarExpandedWidth : sidebarCollapsedWidth;
        
            // Create an animation to change the width
            QPropertyAnimation *animation = new QPropertyAnimation(ui->leftMenu, "maximumWidth", this);
            animation->setDuration(animationDuration);
            animation->setStartValue(ui->leftMenu->maximumWidth());
            animation->setEndValue(targetWidth);
            animation->start();
        
            // Update the collapsed state
            isSidebarCollapsed = !isSidebarCollapsed;
        }
        
        

        In MainWindow.h:

        bool isSidebarCollapsed; // Define the variable here
        
        // Constants for sidebar width and animation duration
        const int sidebarExpandedWidth = 198;  // Adjust this to your needs
        const int sidebarCollapsedWidth = 60;  // Adjust this to your needs
        const int animationDuration = 500;      // Adjust this to your needs
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @Radio1985
        I may be wrong(!), but I think buttons shrink to the width of their container. You may have to put a minimum or fixed width on the buttons to prevent this when the menu is narrow?

        EDIT
        OIC, you are not complaining about the narrow buttons, only about where the Menu label is positioned? Sorry, don't know about that.

        R 1 Reply Last reply
        0
        • JonBJ JonB

          @Radio1985
          I may be wrong(!), but I think buttons shrink to the width of their container. You may have to put a minimum or fixed width on the buttons to prevent this when the menu is narrow?

          EDIT
          OIC, you are not complaining about the narrow buttons, only about where the Menu label is positioned? Sorry, don't know about that.

          R Offline
          R Offline
          Radio1985
          wrote on last edited by
          #3
          This post is deleted!
          1 Reply Last reply
          0
          • R Radio1985

            Hi All,
            I have a simple desktop widget application creating using QT Creator.
            It has a left menu bar with with some buttons and labels. The menu button has an icon as shown below.
            4fd50a71-c61e-4b30-a968-f39b2b274716-image.png

            I want to collapse the left menu bar (i.e blue color frame) to the left when I press the menu button. The way I have implemented, I see that "Menu" label moves to the left and goes on top of the icon as shown below.
            366dca98-b590-4edb-a5cc-fc8ec1487e4e-image.png

            As shown above the "Menu" label goes on top of the button icon after collapse.
            Any idea how I can fix this?

            I am using the following code.
            In mainwindow.cpp: when the menu button clicked:

            void MainWindow::on_menuIcon_clicked()
            {
                // Calculate the target width for the sidebar
                int targetWidth = isSidebarCollapsed ? sidebarExpandedWidth : sidebarCollapsedWidth;
            
                // Create an animation to change the width
                QPropertyAnimation *animation = new QPropertyAnimation(ui->leftMenu, "maximumWidth", this);
                animation->setDuration(animationDuration);
                animation->setStartValue(ui->leftMenu->maximumWidth());
                animation->setEndValue(targetWidth);
                animation->start();
            
                // Update the collapsed state
                isSidebarCollapsed = !isSidebarCollapsed;
            }
            
            

            In MainWindow.h:

            bool isSidebarCollapsed; // Define the variable here
            
            // Constants for sidebar width and animation duration
            const int sidebarExpandedWidth = 198;  // Adjust this to your needs
            const int sidebarCollapsedWidth = 60;  // Adjust this to your needs
            const int animationDuration = 500;      // Adjust this to your needs
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Radio1985 said in Collapsable menu bar moves label.:

            The way I have implemented

            How did you implement this menu bar? Is it your own implementation? Are these button and "Menu" label in a layout?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            R 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Radio1985 said in Collapsable menu bar moves label.:

              The way I have implemented

              How did you implement this menu bar? Is it your own implementation? Are these button and "Menu" label in a layout?

              R Offline
              R Offline
              Radio1985
              wrote on last edited by Radio1985
              #5

              @jsulm
              This is my design and I used QT Creator to draw the UI widgets. Please see a screenshot if this below.
              77fccb01-396b-4b68-8f38-46261a43394c-image.png

              Thanks!

              Axel SpoerlA 1 Reply Last reply
              0
              • R Radio1985

                @jsulm
                This is my design and I used QT Creator to draw the UI widgets. Please see a screenshot if this below.
                77fccb01-396b-4b68-8f38-46261a43394c-image.png

                Thanks!

                Axel SpoerlA Offline
                Axel SpoerlA Offline
                Axel Spoerl
                Moderators
                wrote on last edited by
                #6

                @Radio1985
                I might be wrong, but I don’t see any menu bar here.
                What I see is a frame containing a push button and a label.
                When the main window shrinks, the are getting squeezed together. That’s expected behavior.
                To achieve the desired behavior, set an icon and a text on the push button and drop the label.

                Software Engineer
                The Qt Company, Oslo

                R 1 Reply Last reply
                0
                • Axel SpoerlA Axel Spoerl

                  @Radio1985
                  I might be wrong, but I don’t see any menu bar here.
                  What I see is a frame containing a push button and a label.
                  When the main window shrinks, the are getting squeezed together. That’s expected behavior.
                  To achieve the desired behavior, set an icon and a text on the push button and drop the label.

                  R Offline
                  R Offline
                  Radio1985
                  wrote on last edited by Radio1985
                  #7

                  @Axel-Spoerl
                  I removed the label and added a text on the push button as shown below.
                  579ed9ed-4285-4472-8927-3fbcb944f181-image.png

                  But when my frame shrinks, the button shrinks and the icon also moves to the left.

                  ef88bae4-2aa1-44b9-bfbe-e2de5ceeed81-image.png

                  The behavior I want is the button shrinks until I see only the button icon and I don't see any text labels, something like below.
                  9a527020-bda7-4eba-a447-dd4d71bee869-image.png

                  Any ideas? If you think that this behavior can be achieved in a different way please recommend it or share any resources.

                  Thanks!

                  Axel SpoerlA 1 Reply Last reply
                  0
                  • R Radio1985

                    @Axel-Spoerl
                    I removed the label and added a text on the push button as shown below.
                    579ed9ed-4285-4472-8927-3fbcb944f181-image.png

                    But when my frame shrinks, the button shrinks and the icon also moves to the left.

                    ef88bae4-2aa1-44b9-bfbe-e2de5ceeed81-image.png

                    The behavior I want is the button shrinks until I see only the button icon and I don't see any text labels, something like below.
                    9a527020-bda7-4eba-a447-dd4d71bee869-image.png

                    Any ideas? If you think that this behavior can be achieved in a different way please recommend it or share any resources.

                    Thanks!

                    Axel SpoerlA Offline
                    Axel SpoerlA Offline
                    Axel Spoerl
                    Moderators
                    wrote on last edited by
                    #8

                    @Radio1985
                    Hi,

                    The short answer is probably:
                    pushButton->setStyleSheet("text-align:left;");

                    But I am not so sure if I understand the use case correctly. There is a menu, created manually with buttons. On top of that, there is a button, called menuIcon(confuses me: why not menuButton?), associated with the text "Menu". So what is it supposed to do, when the menu is already there anyway? I would expect the button to expand the menu.
                    For this purpose, I'd rather use a QMenu, like shown in this example.
                    Or a tool button. Or assign a menu to a push button.

                    Software Engineer
                    The Qt Company, Oslo

                    R 1 Reply Last reply
                    1
                    • Axel SpoerlA Axel Spoerl

                      @Radio1985
                      Hi,

                      The short answer is probably:
                      pushButton->setStyleSheet("text-align:left;");

                      But I am not so sure if I understand the use case correctly. There is a menu, created manually with buttons. On top of that, there is a button, called menuIcon(confuses me: why not menuButton?), associated with the text "Menu". So what is it supposed to do, when the menu is already there anyway? I would expect the button to expand the menu.
                      For this purpose, I'd rather use a QMenu, like shown in this example.
                      Or a tool button. Or assign a menu to a push button.

                      R Offline
                      R Offline
                      Radio1985
                      wrote on last edited by
                      #9

                      @Axel-Spoerl

                      Thanks for the suggestion.

                      pushButton->setStyleSheet("text-align:left;");
                      

                      does fix the issue.
                      Yes it does make sense to name it as menuButton. Initially I had a button for the menu icon and then a label separately. So I was using menuIcon for the button name which I should have changed in the new design.
                      I thought of having application related button options on the GUI main window it self rather than on the menu bar.
                      By collapsing the menu bar to the left, I wanted to get more space from the main dash board side. Hope this makes sense.
                      May be I should remove the wording "Menu" to avoid confusion.

                      1 Reply Last reply
                      0
                      • R Radio1985 has marked this topic as solved on

                      • Login

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