Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 9

Qt-interest Archive, March 2002
Interfacing to non-UNICODE libraries?


Message 1 in thread

Hello,

I opted to compile Qt without UNICODE when UNICODE became standard (in
3.0.2) because I don't need UNICODE and because I interface to  number
of other libraries that don't use UNICODE.

However, having just had to edit qfiledialog in 3.0.3 to get it to
compile under Windows with UNICODE not defined[1] I'm feeling rather
nervous that I effectively might be using untested code.

Maybe I should bite the bullet and make the change? I know there is
qt_winQString & qt_winTchar but from the last experiment I did there
seems to be rather more to it than that.

Can anyone offer advice, or point to some advice, about adapting to
UNICODE.

For example, say I want to call

DWORD GetPrivateProfileString(
  LPCTSTR lpAppName,        // section name
  LPCTSTR lpKeyName,        // key name
  LPCTSTR lpDefault,        // default string
  LPTSTR lpReturnedString,  // destination buffer
  DWORD nSize,              // size of destination buffer
  LPCTSTR lpFileName        // initialization file name
);

My existing call is in a wrapper function:

QString SynConfiguration::get(
        const QString& i_section,
        const QString& i_key,
        const QString& i_default,
        const QString& i_repository)
{
    TCHAR szReturn[255];
    int nBytesReturned = sizeof(szReturn);
    strcpy(szReturn,i_default);

    GetPrivateProfileString(i_section, i_key, szReturn, szReturn,
nBytesReturned,i_repository);

    return (szReturn);
}

How would that be with UNICODE?


[1] In Qt 3.0.3, line 5655 in dialogs\qfiledialog.cpp(5655) uses the
variable "file" which only exists if UNICODE was defined.
Reported to qt-bugs.

-- 
 [ signature omitted ] 

Message 2 in thread

Responding to my own question...

David Wright <David.Wright@skyewright.co.uk> wrote:
>Maybe I should bite the bullet and make the change? I know there is
>qt_winQString & qt_winTchar but from the last experiment I did there
>seems to be rather more to it than that.
>
>Can anyone offer advice, or point to some advice, about adapting to
>UNICODE.

After posting the above, a google search on UNICODE LPCTSTR BSTR came up
with a host of useful resources. The biggest pain seems to be allowing
for handling win95/98, but I think I now have it cracked.

The solution so far as applied to the specific example question I asked
is something like:

QString SynConfiguration::get(
        const QString& i_section,
        const QString& i_key,
        const QString& i_default,
        const QString& i_repository)
{
#if defined(Q_WS_WIN)
    #if defined(UNICODE)
        #ifndef Q_OS_TEMP
            if ( qWinVersion() & Qt::WV_NT_based ) {
        #endif
            WCHAR szSection[255];
            wcscpy( szSection, (TCHAR*)qt_winTchar( i_section, true ) );
            WCHAR szKey[255];
            wcscpy( szKey, (TCHAR*)qt_winTchar( i_key, true ) );
            WCHAR szReturn[255];
            int nBytesReturned = sizeof(szReturn);
            wcscpy( szReturn, (TCHAR*)qt_winTchar( i_default, true ) );

            GetPrivateProfileStringW(szSection, szKey, szReturn,
szReturn, nBytesReturned, (TCHAR*)qt_winTchar( i_repository, true ));

            return (qt_winQString(szReturn));
        #ifndef Q_OS_TEMP
            } else
        #endif
    #endif
    #ifndef Q_OS_TEMP
        {
            char szReturn[255];
            int nBytesReturned = sizeof(szReturn);
            strcpy(szReturn,i_default);
            GetPrivateProfileStringA(i_section, i_key, szReturn,
szReturn, nBytesReturned,i_repository);

            return (szReturn);
        }
    #endif
#else
    Unexpected!
#endif
}

I'm not quite certain of the purpose of Q_OS_TEMP, so I've basically
just used it in the way it seems to be used in Qt's own code. Does
anyone know its real purpose?

-- 
 [ signature omitted ]