Qt 多语言国际化 tr()的一点注意事项
在QTreeView的model中我使用QStandardItem 构建,当获取QStandardItem的text字段后,不能够按照原文进行匹配,需要把做判定匹配的字符串同时加上tr(),才能够根据翻译前的内容进行匹配。
错误示例:
/*获取的字符串*/
if (index.isValid()) { QStandardItemModel *model = qobject_cast<QStandardItemModel *>(treeView->model()); if (model) { QStandardItem* boxName = model->itemFromIndex(index); if(boxName->parent()&&boxName->parent()->parent()&&boxName->parent()->parent()->parent()){ QStandardItem* stationName=boxName->parent()->parent(); if (stationName) { emit userClickedContentID(stationName->text(),
boxName->parent()->text(),boxName->text()); } } } }
/*获取后需要判定的字符串*/
if(typeName=="Edge Box:"){
...
}else if(typeName=="Comm Box:"){
...
}
如果需要正常判定,常量字符串也要加上tr()。
if(typeName==tr("Edge Box:")){
... }else if(typeName==tr("Comm Box:")){ ...
}

浙公网安备 33010602011771号