[SOLVED] QComboBox "addAction" not working?



  • Hi guys,

    I did the tutorial to make my application multi-language
    http://qt-project.org/wiki/How_to_create_a_multi_language_application
    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):
    https://www.dropbox.com/s/iiyjczl02aatles/QMenu.png

    With QComboBox (what I would like, not working):
    https://www.dropbox.com/s/v5v0zdr067cmrot/QComboBox.png

    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);
    langGroup->setExclusive(true);

    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;
    m_langPath.append("/languages");
    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);
        action->setCheckable(true);
        action->setData(locale);
    
        ui->menuLangage->addAction(action);
        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...
    langGroup->addAction(action);

        // set default translators and language checked
        if (defaultLocale == locale) {
            action->setChecked(true);
        }
    }
    

    }@



  • Take a look into the "documentation of QComboBox":http://qt-project.org/doc/qt-5.1/qtwidgets/qcombobox.html . 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.