我是一个小初学者,我在 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
或者更好 accepts QString 也是如此。
plik << QObject::tr("Example2"); // will do
关于c++ - 我无法在 Qt 5.5 中的 Qt 类之外使用 tr(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32683678/