QMenu does not properly adjust to size of contents
-
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.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; }
-
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; }
-
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; }
@Christian-Ehrlicher perfect, thank you!
-