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. QMenuBar does not show up

QMenuBar does not show up

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 397 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.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • S Offline
        S Offline
        sandro4912
        wrote on last edited by
        #3

        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 added

        mLanguageMenu->setTitle(tr("Language"));
        

        in the void MainWindow::createLanguageMenu() method and now it works fine.

        1 Reply Last reply
        0

        • Login

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