
Originally Posted by
anda_skoa
str.toAscii() will obviously do something very wrong if str does not contain ASCII.
So you convert the filename to local 8 bit encoding, which, according to you is UTF-8, then you transfer it back into 16bit Unicode and have qDebug() again encode it into 8 bit?
Have you had a look at QString::toUtf8()?
That is a short form of using th UTF-8 text codec and calling its fromUnicode() function.
Cheers,
_
Sorry, I probably should state more clear.
I try to read an ini file using QSettings, the ini file contains GB2312 text.
I want to read this file to work in all locale settings. The application may have done QTextCodec::setCodecForLocale( QTextCodec::codecForName( "GB2312" ) ), or may not have done that, in which case it is Linux English locale by default.
The ini is like
xxx=yyy
which xxx is in GB2312, and yyy is also in GB2312. yyy is pointing to a file path, which I need to open the file to read.
My approach is to decode both in UTF8 because the actual file name is in UTF8.
Here is what I have done. I don't like temporarily set and restore the locale. It looks ugly.
struct MyInfo {
};
static QString toUnicode
( const QString
& str
) { return QTextCodec::codecForName( "GB2312" )->toUnicode
( str.
toLatin1() );
}
* static MyInfo parseValue( const QString& key, const QStringList& strList ) {
MyInfo info;
info.key = key;
info.filename = strList.first():
return info;
}
static QMap<QString, MyInfo> info;
static void readIni( const QString& iniFileName ) {
if( QFile::exists( iniFileName
) ) {
// save codec
// save codec utf-8 for this file
settings.
setIniCodec( QTextCodec::codecForName( "GB2312" ) );
settings.beginGroup( tag );
{
foreach( const QString& key, settings.allKeys() ) {
QStringList strList
= settings.
value( key
).
toStringList();
QString localKey
= toUnicode
( key
);
MyInfo info = parseValue( localKey, strList );
info[ info.name ] = info;
}
}
settings.endGroup();
// restore codec (this looks ugly)
}
...
}
// after reading the ini, I have to do like this:
static QImage createImage
( const QString
& key
) {
// save codec
QImage image
( info
[ key
].
filename );
// restore codec (this looks ugly)
return image;
}
struct MyInfo {
QString key;
QString filename;
};
static QString toUnicode( const QString& str ) {
return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() );
}
* static MyInfo parseValue( const QString& key, const QStringList& strList ) {
MyInfo info;
info.key = key;
info.filename = strList.first():
return info;
}
static QMap<QString, MyInfo> info;
static void readIni( const QString& iniFileName ) {
if( QFile::exists( iniFileName ) ) {
// save codec
QTextCodec* codec = QTextCodec::codecForLocale();
// save codec utf-8 for this file
QTextCodec::setCodecForLocale( QTextCodec::codecForName( "utf8" ) );
QSettings settings( iniFileName, QSettings::IniFormat );
settings.setIniCodec( QTextCodec::codecForName( "GB2312" ) );
settings.beginGroup( tag );
{
foreach( const QString& key, settings.allKeys() ) {
QStringList strList = settings.value( key ).toStringList();
QString localKey = toUnicode( key );
MyInfo info = parseValue( localKey, strList );
info[ info.name ] = info;
}
}
settings.endGroup();
// restore codec (this looks ugly)
QTextCodec::setCodecForLocale( codec );
}
...
}
// after reading the ini, I have to do like this:
static QImage createImage( const QString& key ) {
// save codec
QTextCodec* codec = QTextCodec::codecForLocale();
QTextCodec::setCodecForLocale( QTextCodec::codecForName( "utf8" ) );
QImage image( info[ key ].filename );
// restore codec (this looks ugly)
QTextCodec::setCodecForLocale( codec );
return image;
}
To copy to clipboard, switch view to plain text mode
Bookmarks