0

I have QT_NO_CAST_FROM_ASCII defined so I cannot cast from a char* to a QString. Previously I've been using: QLatin1String
But I just noticed QString::fromStdString, I don't like using this because QLatin1String is preferred to QString::fromLatin1.

Does anyone know of a Qt object for std::strings that works like QLatin1String?

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • `QLatin1String` is just wrapper around `const char *` c-string. So I have no idea what you want. Do you want convert to `QString`? Or do you what just `QLatin1String`. – Marek R May 13 '14 at 13:38
  • @MarekR `QLatin1String` is to `char*` as `?` is to `std::string`. I wanna know what the `?` is. – Jonathan Mee May 13 '14 at 13:42
  • 1
    There is no Qt object that matches `?`. The official method for converting std::string to QString is `QString::fromStdString()`. What do you mean by "`QLatin1String` is preferred to `QString::fromLatin1`"? – JKSH May 14 '14 at 03:13
  • It would be best if you paste here code you have and code you want to have. Note: you are making common mistake asking how to fix your solution of some problem instead describe the problem you are trying to solve. Final outcome is that we have no idea what is the problem. – Marek R May 14 '14 at 08:12
  • @JKSH I guess, there isn't a wrapper for this is the official answer then. If you put that in an answer I'll accept it; with much disappointment. As far as QLatin1String being preferred, check here: http://qt-project.org/doc/qt-5/qlatin1string.html#details – Jonathan Mee May 14 '14 at 11:07
  • How is `QT_NO_CAST_FROM_ASCII` relevant? It just forces you to be explicit, instead of having implicit default. If you want the behavior you would have without it, read from docs what happens then and use the right explicit method. If you want `QString`, create `QString`. If not, consider just keeping your `std::string`, nothing wrong with that in Qt code, it's C++... – hyde May 14 '14 at 14:27
  • @hyde `QT_NO_CAST_FROM_ASCII` forces you to use Qt's thin wrappers: `QLatin1String` for `const char*`. The relevance to this is that I want a thin wrapper for `std::string`s so I don't have to call `c_str` before using Qt's wrapper. – Jonathan Mee May 14 '14 at 15:40
  • @JonathanMee I've read the link, but I still don't really understand why you don't like using `QString::fromStdString()`. Is it for efficiency, to avoid creating copies? Like Marek R said, please edit your question and explain: What are you trying to do? Why do you need these wrappers? If you explain that clearly, maybe we can find an even better solution. – JKSH May 15 '14 at 01:15
  • @JKSH I've written up an answer which summarizes the problem and cites your statement that there isn't a wrapper for `std::strings` – Jonathan Mee May 15 '14 at 11:48

1 Answers1

2

For those who use Qt with QT_NO_CAST_FROM_ASCII enabled char*s and std::strings cannot be assigned to a QString or cast to a QString.

This will not compile with QT_NO_CAST_FROM_ASCII enabled:

QString foo( "Hello World" );

You could do this but Qt frowns upon it because it creates, "temporary QString objects and make a deep copy of the character data":

QString foo( QString::fromLatin1( "Hello World" ) );

Instead you have to use:

QString foo( QLatin1String( "Hello World" ) );

It's a little more frustrating if you need to get a QString from a std::string because you have to now call:

QString foo( QLatin1String( helloWorld.c_str() ) );

There is a direct conversion but not with Qt's recommended wrappers:

QString foo( QString::fromStdString( helloWorld ) );

That is to say I have not been able to find a wrapper for std::string and JKSH asserts that there isn't one. So I believe that as of Qt 5.2 the preferred solution is to use the QLatin1String wrapper.



(As an aside for those interested in multilingual software, Qt's QLatin1String and QString::fromLaint1 both expect the char* to be in proper unicode; QString::fromStdString actually uses QString::fromUtf8 under the hood to convert to proper unicode.)

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288