我不能在Qt类之外的Qt 5.5中使用tr()
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
相关文章:
- Qt VTK交互风格的信号到小部件
- QT在错误的班级中寻找空位
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在Qt C++中更改光标
- 如何在MS Visual Studio 2019中运行QT UI
- QT通过C++添加映射QML项目
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用Qt C++计算类似Git的SHA1哈希
- Qt focusObjectChanged example?
- 如何在cpp文件之间切换窗口?在Qt中
- Qt和C++:将QLineEdit添加到QTabWidget中
- 从数据库实时显示QT c++中的数据
- Qt Quick-如何仅从c++代码与qml属性交互
- tr( "302261" ) 在 Qt 中是什么意思?
- tr()函数不再起作用,从QT 4到QT 5迁移
- 如果我在静态成员中使用,Qt tr() 不起作用
- 我不能在Qt类之外的Qt 5.5中使用tr()