[Interest] Converting std::string.c_str() code to use QString methods

Michael Jackson imikejackson at gmail.com
Sat Aug 31 23:27:55 CEST 2013


On Aug 31, 2013, at 12:51 PM, Thiago Macieira <thiago.macieira at intel.com> wrote:

>> 
>> 3) We use the HDF5 (www.hdfgroup.org) to store files. The entire interface
>> is "C" and uses char* to get strings into and out the file when needed. Of
>> all the discussion going on this is the part that worries me the most. Do I
>> use "toAscii().data()" or toLatin().data()? We are still using Qt 4.8.4 but
>> in the next year will probably move to Qt 5. I don't want to rewrite 500
>> source files with APIs that are going to be deprecated in what we move up
>> to. I'd only like to do this transition once.
> 
> Let's split in two:
> 
> 1) file names
> 
> The correct thing to do is to use QString and *never* convert your file names 
> to 8-bit. All the Qt API supports file names in this form, including QFile.
> 
> If you must convert to a non-Qt type, the best type is const char16_t*:
> 
> 	some_function(reinterpret_cast<const char16_t *>(string.utf16());
> 
> Otherwise, you can use const wchar_t* (by using toWCharArray()).
> 
> If you must convert to 8-bit, you should use QFile::encodeName(). That's 
> equivalent to .toLocal8Bit() on all systems today. Having anything that differs 
> is a major headache.
> 
> 2) user text that is not a file name
> 
> Again, keeping it in QString is the best solution. Otherwise, if your other 
> library has Unicode entry points, you should use them (toUtf8, utf16(), 
> toUcs4(), toWCharArray()).
> 
> If the interface only supports local 8-bit, you should throw the library away 
> for being too limited.
> -- 
> Thiago Macieira - thiago.macieira (AT) intel.com
>  Software Architect - Intel Open Source Technology Center

Well, unfortunately I can not remove HDF5 as or entire file format is based on it. So with that limiting factor in place and knowing that I may have a file path coming from a standard Open/Save Dialog how would _you_ handle this situation? These are my design parameters that I have and so I just want to figure out the way that will cause the least amount of headaches.

On the plus side of things HDF5 does have a C++ API where some of the class methods will take a std::string but I am not sure this is going to help in this case.

I have taken all the other bits of advice and am in the process of removing all uses of FILE and std::iostreams with Qt equivalents. I do appreciate all the help. I have an eye towards getting our application translated in the future which is one of the reasons I decided to rewrite most of our lower level libraries with Qt.

Again, all the comments are extremely helpful and I am feverishly taking notes.

Cheers
Mike Jackson


More information about the Interest mailing list