21

I'm currently working on a larger project, where the "logic" is implemented in standard C++ with all strings being handled with std::wstring and the UI part is implemented using Qt and thus necessarily QString (Bonus question: is this true?).

What is the best way to connect those two worlds?

I know I can use something like

std::wstring wideString;
QString qtString = QString::fromStdWString(wideString);

but I'm wondering if there is a nicer way with less typing involved. A user defined operator came to my mind, but I'm not experienced enough to tackle that on my own.

Would be glad if anyone could point me in the right direction.

mort
  • 12,988
  • 14
  • 52
  • 97
  • 4
    Have you ever read [QString, std::wstring and built-in wchar_t](http://qt-project.org/wiki/toStdWStringAndBuiltInWchar)? – Jesse Good Feb 06 '13 at 10:12
  • You've accepted an answer which described only the conversion in one direction. Was that your question? Then you should probably modify the title so that it is not misleading (for those hoping to find an answer about the conversion in the other direction). – imz -- Ivan Zakharyaschev Jul 02 '15 at 10:19

3 Answers3

23

It's a good idea to use QString::fromStdWString but (!!!) if Qt was compiled with exactly the same STL headers as your project. If not - you can get a lot of fun, catching a bug.

If you don't sure that both STL headers are the same use QString::fromWCharArray:

std::wstring wideString;
QString qtString = QString::fromWCharArray( wideString.c_str() );

Update: answering to @juzzlin:
Lets imagine that Qt was build with the STL containing the following std::wstring:

class wstring { // I know, that there's no such class, but I'm shure you'll understand what I want to say
    wchar_t * m_ptr;
    size_t m_length;
    ...
};

and you have the STL containing the following std::wstring:

class wstring {
    wchar_t * m_ptr;
    wchar_t * m_the_end;
    ...
};

If you'll give your std::wstring to Qt, it will interpret m_the_end pointer as the length of the string, and

you can get a lot of fun, catching a bug

borisbn
  • 4,988
  • 25
  • 42
  • 1
    What would happen if "exactly the same" STL headers are not used? And why is QString::fromWCharArray() a better solution is that case? – juzzlin Mar 12 '15 at 21:45
  • 3
    fromStdWString is inlined in qstring.h and has implementation: `return fromWCharArray(s.data(), int(s.size()));` So, it is safe to use it. – rmflow Oct 08 '15 at 14:03
  • @rmflow Yes, you're right, but there's no guarantee that `wstring::data` and `wstring::size` are inlined too. – borisbn Oct 12 '15 at 13:56
  • 1
    @borisbn it does not matter, since they are compiled from header (in your code). You could write by yourself `fromWCharArray(s.data(), int(s.size()));` instead of calling `fromStdWString` and result would be the same. – rmflow Oct 12 '15 at 14:53
  • @borisbn, could you update the answer, please? – Sasha Jan 12 '22 at 16:36
2

I think a user defined conversion is what you're looking for, and from what I can gather, it should look something like this:

class foo
{
public:
   operator QString(std::wstring& ws)
   {
       return QString::fromStdWString(ws);
   }
}
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
0

maybe make a inline function QString toQString(std::wstring string) to make it 'less to type' ...

but to be honest ... thats not the big effort at all to write it from time to time ;)

soo long zai

Zaiborg
  • 2,492
  • 19
  • 28