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. QMenu does not properly adjust to size of contents
QtWS25 Last Chance

QMenu does not properly adjust to size of contents

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 143 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.
  • S Offline
    S Offline
    sapvi
    wrote on 7 Apr 2025, 08:37 last edited by sapvi 4 Jul 2025, 08:37
    #1

    Hello. I am facing with this problem: for some reason, the width of the QMenu is automatically adjusted but the result is just not correct. It introduces too big gap between text of an item and the corresponding shortcut, meaning that the rightmost part of the shortcut is cut and useless. It does not behave like this if I have just text without shortcut: for such situations, QMenu just grows properly to fit the content. Here, while the width is changed based on the content, it is just not sufficient. For smaller font sizes, it actually fits properly, but for a bit bigger once, I get situation like on the screenshot below.

    89657286-ac81-46c0-8d83-6a3d3d177481-image.png

    I do not set any width manually:

        QMenuBar {
            background-color: #33281F;  
            color: #F5F4F4; 
            height: 26px; 
            font-size: 18px; 
        }
        QMenuBar::item {
            background-color: #33281F; 
            padding: 0px 5px 0px 5px; 
        }
        QMenuBar::item:selected {
            background-color: #DDEAED; 
            color: #33281F;
        }
        QMenu {
            background-color: #33281F;
            color: #F5F4F4; 
            border: 1px solid #222; 
        }
        QMenu::item {
            background-color: #33281F; 
            color: #F5F4F4; 
            padding: 4px 10px; 
            font-size: 18px; 
        }
        QMenu::item:selected {
            background-color: #DDEAED; 
            color: #33281F; 
        }
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 7 Apr 2025, 11:20 last edited by Christian Ehrlicher 4 Jul 2025, 11:47
      #2

      Please create a bug report with this minimal compilable example:

      int main(int argc, char* argv[])
      {
          QApplication app(argc, argv);
          app.setStyleSheet(R"(
          QMenu::item {
              font-size: 18px; 
          }
      )");
          QMainWindow mw;
          auto m = mw.menuBar()->addMenu("Menu1");
          auto a1 = new QAction("&Increase");
          a1->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Plus));
          m->addAction(a1);
          auto a2 = new QAction("&Decrease");
          a2->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Minus));
          m->addAction(a2);
          mw.show();
          return app.exec();
      }
      

      /edit: no need for a bug report. You need to set the font size on the QMenu too as this calculates the shortcut width:

          QMenu {
              font-size: 18px; 
          }
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply 8 Apr 2025, 11:52
      1
      • C Christian Ehrlicher
        7 Apr 2025, 11:20

        Please create a bug report with this minimal compilable example:

        int main(int argc, char* argv[])
        {
            QApplication app(argc, argv);
            app.setStyleSheet(R"(
            QMenu::item {
                font-size: 18px; 
            }
        )");
            QMainWindow mw;
            auto m = mw.menuBar()->addMenu("Menu1");
            auto a1 = new QAction("&Increase");
            a1->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Plus));
            m->addAction(a1);
            auto a2 = new QAction("&Decrease");
            a2->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Minus));
            m->addAction(a2);
            mw.show();
            return app.exec();
        }
        

        /edit: no need for a bug report. You need to set the font size on the QMenu too as this calculates the shortcut width:

            QMenu {
                font-size: 18px; 
            }
        
        S Offline
        S Offline
        sapvi
        wrote on 8 Apr 2025, 11:52 last edited by
        #3

        @Christian-Ehrlicher perfect, thank you!

        1 Reply Last reply
        0
        • S sapvi has marked this topic as solved on 8 Apr 2025, 11:52

        1/3

        7 Apr 2025, 08:37

        • Login

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