PDA

View Full Version : Using Multi-Byte in place of Unicode



TheDuncan
11th March 2010, 18:56
I have some code that was written for windows. One of the parameters requires multi-byte instead of unicode. In Visual Studio 2008 I can change that in Project->Class Properties->Configuration Set. Is it possible to do this in Qt 4? The project I am working on right now requires Multi-Byte input vs Unicode, but I cannot seem to change/convert it to work right.

Thanks

wysota
11th March 2010, 20:09
Qt uses Unicode internally, I doubt you can change that. But converting to wchar* (or std::wstring) should be possible (QString::toStdWString()).

TheDuncan
11th March 2010, 20:18
Thanks for the speedy reply sir!
Buuuuut it didn't work as easy as I had hoped .

Here is the code that is breaking.



char adjust_device[16];
sprintf(adjust_device, "\\\\.\\%s", device);
HComm = CreateFile(adjust_device, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (HComm == INVALID_HANDLE_VALUE) {
return -1;
}


I tried to do this:


sprintf(QString::toStdWString(adjust_device), "\\\\.\\%s", device);
HComm = CreateFile(adjust_device, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);


and I get the following error: no matching function call toQString::toStdWstring(char[16])

Any ideas?

wysota
11th March 2010, 20:51
It's not a static method, that's one thing. The other is that it will convert the string to std::wstring, just as the name of the method says and not to a char*. The device name probably contains ascii characters only anyway, so you can probably do this:


QString device = "<something goes here">;
QString adjustDeviceStr = "\\\\.\\"+device;
QByteArray ba = adjustDeviceStr.toAscii();
HComm = CreateFile(ba.constData(), ...);

squidge
11th March 2010, 22:31
There's two versions of CreateFile:
CreateFileA takes a ASCII parameters (char *)
CreateFileW takes Unicode parameters (wchar *)

Typically, if you have Unicode defined, CreateFileW is aliases to CreateFile, else CreateFileA is aliased to CreateFile.

You can still use either by using the proper names:



QString foo = "\\\\.\\COM1";
CreateFileW(foo.utf16(), ...)




char foo[] = "\\\\.\\COM1";
CreateFileA(foo, ...)

TheDuncan
12th March 2010, 16:35
Thanks a bunch. I used CreateFileA and it seems to be working.

I'm new to Qt so I have a noob question to ask. How can I display a long in a Line Edit? I tried this



QString number(lmsDist[20], 10);
ui->txtAhead->setText(number);


but it just inserts blank spaces. I used debugging mode to check what lmsDist[540] was and it was an actual number (6530, 87, etc). What am I doing wrong?

Thanks again with your help.

wysota
12th March 2010, 16:43
See QString::number().

TheDuncan
12th March 2010, 16:58
Works like a champ. Thanks a million!