Important: Please read the Qt Code of Conduct -

[SOLVED] QComboBox "addAction" not working?

  • Hi guys,

    I did the tutorial to make my application multi-language
    It's working fine so far, but I would like to put the language option in a "QComboBox" instead of a "QMenu" and this is where I am struggling.

    With QMenu (Working):

    With QComboBox (what I would like, not working):

    With the same code from the tutorial, I just switch the QMenuBar for my QComboBox, but the QComboBox doesn't get populated with QAction? Maybe I don't get something because QComboBox has a "addAction" method also, is it deprecated?
    Thanks in advance!

    void MainWindow::createLanguageMenu() {

    qDebug() << "createLanguageMenu -----------------------";

    // QActionGroup* langGroup = new QActionGroup(ui->menuLangage);
    QActionGroup* langGroup = new QActionGroup(ui->comboBox_language);

    connect(langGroup, SIGNAL(triggered(QAction *)), this, SLOT(slotLanguageChanged(QAction *)));
    // format systems language
    QString defaultLocale = QLocale::system&#40;&#41;.name(&#41;;       // e.g. "de_DE"
    defaultLocale.truncate(defaultLocale.lastIndexOf('_')); // e.g. "de"
    QString     m_langPath = QApplication::applicationDirPath();
    qDebug() << "DIR PATH :" << m_langPath;
    qDebug() << "DIR PATH2 :" << m_langPath;
    QDir dir(m_langPath);
    QStringList fileNames = dir.entryList(QStringList("powervelo_*.qm"));
    foreach (QString filename, fileNames) {
        QString locale = filename;
        qDebug() << "Fichier: " << locale;
        locale.truncate(locale.lastIndexOf('.'));   // "TranslationExample_de"
        qDebug() << "Fichier: " << locale;
        locale.remove(0, locale.indexOf('_') + 1);   // "de"
        qDebug() << "Fichier: " << locale;
        QString lang = QLocale::languageToString(QLocale(locale).language());
        qDebug() << "Lang: " << lang;
        QIcon ico(QString("%1/%2.png").arg(m_langPath).arg(locale));
        QAction *action = new QAction(ico, lang, this);
        ui->comboBox_language->addAction(action);   // Item is not added to ComboBox like QMenu?

    // ui->comboBox_language->addItem(ico, lang); This one works, but no action...

        // set default translators and language checked
        if (defaultLocale == locale) {


  • Take a look into the "documentation of QComboBox": . The function you need is "addItem". "addAction" stems from QWidget and is designed and used to populate context menus - in case of QMenu (or QToolBar) it was overwritten to implement custom behavior. If you use it with your combobox, however, it will not at all do what you expect it to.

  • Thanks for your answer, I will use setItem and not use QAction instead just monitor the QComboBox with
    @void MainWindow::on_comboBox_language_currentIndexChanged(int index) { }@

Log in to reply