Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] How to get QLocale by QLocale::Country



  • Hi Al!!
    I have a list of countries such a:
    @for (int i = 1; i < QLocale::LastCountry-1; ++i) {
    //here i need to display native country name, how can i do that?
    }@



  • Hi there. Is "that":http://qt-project.org/doc/qt-5/qlocale.html#countryToString what you want?
    @
    for (int i = 1; i < QLocale::LastCountry-1; ++i)
    {
    qDebug()<<QLocale::countryToString((QLocale::Country)i);
    }
    @



  • Hi,

    I'm not at home so I can't try this right now, but I would try something like this:
    @QList<QLocale> allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);

    for (int i = 1; i < allLocales. count()-1; ++i) {
    QString nativeCountryName = allLocales.at(i).nativeCountryName();
    }@



  • [quote author="Jan-Willem" date="1400243997"]
    @
    for (int i = 1; i < allLocales. count()-1; ++i) {
    QString nativeCountryName = allLocales.nativeCountryName();
    }@
    [/quote]
    you forgot iterator:
    @
    QString nativeCountryName = allLocales.at(i).nativeCountryName();
    @



  • Yep your right. I will update the code.
    Thanks



  • [quote author="Jan-Willem" date="1400243997"]Hi,

    I'm not at home so I can't try this right now, but I would try something like this:
    @QList<QLocale> allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);

    for (int i = 1; i < allLocales. count()-1; ++i) {
    QString nativeCountryName = allLocales.at(i).nativeCountryName();
    }@
    [/quote]

    A lot of tnx! but this code give around 400 records, when countries count around 250, how can i filter them?



    • i don't need all of languages at the country, i need only one language per country.


  • and one more, is any body know how can i take a phone number code by CountryCode? Maybe some one know free lib for that?



    • Perhaps you could experiment with the values of QLocale::AnyLanguage and QLocale::AnyScript to limit you output.

    • You could check if nativeCountryName already exists, and when true, don't add it to your list.

    • Alternatively you could use QMetaEnum to retrieve the values of the enum QLocale::Country, where al the country names are stored. The names of the countries are returned by key().



  • i currently use map for set relation from QLocale::Country to QLocale, this is solve my issue.
    Once again tnx!
    about taking phone number code issue is open.


Log in to reply