我想熟悉qml controle组件的语言环境属性,我知道它用于Qlocale方法,但是我找不到任何好的示例
答案 0 :(得分:1)
如果要更改特定控件上使用的语言环境,可以导出C ++模型上的字符串属性:
class MyModel : public QObject
{
Q_OBJECT
Q_PROPERTY(QString editLocale MEMBER editLocale_) //more elaborate variants are possible
private:
QString editLocale_;
}
在main()中导出模型:
QQuickView view;
MyModel theModel;
view.rootContext()->setContextProperty("theModel", &theModel);
在QML中按以下方式使用它:
CheckBox {
onClicked: {
checked = !checked
if(checked)
theModel.editLocale = "nl_NL"
else
theModel.editLocale = "en_US"
}
SpinBox { //this is derived from the QtQuick.Controls 2.5 Control type you are looking at
locale: Qt.locale(theModel.editLocale)
to: 2000
value: 1000
}
在此示例中,切换复选框时,您应该在点和逗号之间看到千位分隔符。
警告 :由于我的环境无法识别locale
属性,因此未测试此代码