[英]I can't use tr() in Qt 5.5 outside the Qt classes
我是一個小初學者,我在Qt中實現tr函數時遇到了問題。 如果我在Qt類之外使用tr(“string”),我會收到錯誤。 我發現信息,我應該在tr()之前使用QObject ::但是如果我嘗試使用它
temp += QObject::tr("Example"); // temp is std::string
我收到了錯誤
C2679: binary '+=' : no operator found which takes a right-hand operand of type 'QString' (or there is no acceptable conversion)
另一個例子:
QString filename="example.txt";
QFile log(QDir::currentPath() + "//" + filename);
if ( log.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text) )
{
QTextStream plik( &log );
plik.setCodec("UTF-8");
(...)
plik << QString::fromUtf8(QObject::tr("Example2")); // Error
}
我收到了錯誤
C2665: 'QString::fromUtf8' : none of the 2 overloads could convert all the argument types
任何人都可以幫我解決這個問題嗎?
Qt有很多訪問器和QString :: toStdString() 。
temp += QObject::tr("Example").toStdString(); // temp is std::string
對於流需要轉換為Utf8字節數組:
plik << QObject::tr("Example2").toUtf8(); // fixed
或者更好的是它也接受 QString。
plik << QObject::tr("Example2"); // will do
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.