km2442
km2442

Reputation: 827

I can't use tr() in Qt 5.5 outside the Qt classes

I'm a little beginner, and I've a problem with implementation of the tr function in Qt. If I'm using tr("string") outside the Qt classes I'm getting errors. I found information, that I should use QObject:: before the tr(), but If I try to do it with

temp += QObject::tr("Example"); // temp is std::string

I'm getting error

C2679: binary '+=' : no operator found which takes a right-hand operand of type 'QString' (or there is no acceptable conversion)

Another example:

    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
    }

I'm getting error

C2665: 'QString::fromUtf8' : none of the 2 overloads could convert all the argument types

Could anyone help me with this issue?

Upvotes: 2

Views: 1768

Answers (1)

Alexander V
Alexander V

Reputation: 8698

Qt has so many accessors and QString::toStdString() as well.

temp += QObject::tr("Example").toStdString(); // temp is std::string

for the stream need to convert either to Utf8 byte array:

plik << QObject::tr("Example2").toUtf8(); // fixed

or even better it accepts QString as well.

plik << QObject::tr("Example2"); // will do

Upvotes: 4

Related Questions