localeselector.cpp Example File
widgets/validators/localeselector.cpp
#include "localeselector.h"
#include <QLocale>
LocaleSelector::LocaleSelector(QWidget *parent)
: QComboBox(parent)
{
int curIndex = -1;
int index = 0;
for (int _lang = QLocale::C; _lang <= QLocale::LastLanguage; ++_lang) {
QLocale::Language lang = static_cast<QLocale::Language>(_lang);
foreach (const QLocale &l, QLocale::matchingLocales(lang, QLocale::AnyScript, QLocale::AnyCountry)) {
QString label = QLocale::languageToString(l.language());
label += QLatin1Char('/');
label += QLocale::countryToString(l.country());
if (QLocale::matchingLocales(l.language(), QLocale::AnyScript, l.country()).size() > 1)
label += QLatin1String(" (") + QLocale::scriptToString(l.script()) + QLatin1Char(')');
addItem(label, QVariant::fromValue(l));
if (l.language() == locale().language() && l.country() == locale().country()
&& (locale().script() == QLocale::AnyScript || l.script() == locale().script())) {
curIndex = index;
}
++index;
}
}
if (curIndex != -1)
setCurrentIndex(curIndex);
connect(this, SIGNAL(activated(int)), this, SLOT(emitLocaleSelected(int)));
}
void LocaleSelector::emitLocaleSelected(int index)
{
QVariant v = itemData(index);
if (!v.isValid())
return;
const QLocale l = v.value<QLocale>();
emit localeSelected(l);
}