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. [SOLVED] QComboBox "addAction" not working?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QComboBox "addAction" not working?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 6.8k 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    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);
        }
    }
    

    }@


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thEClaw
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        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) { }@


        Free Indoor Cycling Software - https://maximumtrainer.com

        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