Solved QMenuBar does not show up
-
I ty to make a menuBar to change the language of my Application at run time. However the bar does not show up.
Here is a snippet how i create the bar:
class MainWindow : public QMainWindow { Q_OBJECT //... private: //... QMenu *mLanguageMenu; };
void MainWindow::createMenus() { qDebug() << menuBar()->actions().size(); // here its zero createLanguageMenu(); menuBar()->addMenu(mLanguageMenu); qDebug() << menuBar()->actions().size(); // here size is 1 so it was added }
void MainWindow::createLanguageMenu() { mLanguageMenu = new QMenu{ this }; mLanguageActionGroup = new QActionGroup{ this }; connect(mLanguageActionGroup, &QActionGroup::triggered, this, &MainWindow::switchLanguage); QDir qmDir{":/translations"}; auto fileNames = qmDir.entryList(QStringList("quiz_*.qm")); for (auto i=0; i < fileNames.size(); ++i) { auto locale = fileNames[i]; locale.remove(0, locale.indexOf('_') + 1); locale.chop(3); QTranslator translator; translator.load(fileNames[i], qmDir.absolutePath()); auto language = translator.translate("MainWindow", "English"); auto action = new QAction{tr("&%1 %2").arg(i + 1).arg(language), this}; action->setCheckable(true); action->setData(locale); mLanguageMenu->addAction(action); mLanguageActionGroup->addAction(action); if (language == "English") { action->setChecked(true); } } }
At first i thougth this was maybe a bug in KDE Neon but then i also compiled the code with Windows 10 but its the same result. Any Idea what i do wrong? As you can see in the
qDebug()
statements it looks like the action gets added but does not show. -
Hi,
Might be a silly question but did you check the number of actions added to your menu ?
What version of Qt are you using ?
What if you create your menu using the QMenuBar::addMenu overload that takes a string ? -
i foudn the issue. I added annother string with the string constructor as you recommended.
The menu showed up. Then I realized that I don't give my menu a name. So i addedmLanguageMenu->setTitle(tr("Language"));
in the
void MainWindow::createLanguageMenu()
method and now it works fine.