c++ - 我无法在 Qt 5.5 中的 Qt 类之外使用 tr()

标签 c++ qt tr

我是一个小初学者,我在 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/

相关文章:

bash - 替换除字母以外的所有内容

ubuntu - 如何用一个字符替换多个字符?

c++ - 一种在 Mac 上强制关闭模态 QFileDialog 的方法

bash - 从字符串中查找并替换大写字符并保留原始匹配项

c++ - 欧拉项目 - 03 级

c++ - 类和结构的高效 push_back

python - 如何在 headless 脚本中使用QProcess?

c++ - QSqlQuery 没有正确回答

c++ - 为什么 std::is_array 对 std::array 返回 false?

c++ - gcc vs. clang: "invalid use of incomplete type"with std::declval 和模板特化