//читаю файлQByteArray ba;//поскипаноba = file.readAll();
QTextCodec *codec = QTextCodec::codecForLocale();QString string = codec->toUnicode(ba);
QString string = QString::fromUtf8(ba);
bool isUtf8(const char *str){ unsigned int len = strlen (str); unsigned int counter = 0; unsigned char head = 0x80; unsigned char tempStr; unsigned int gbNum = 0,utf8Num = 0; bool gb2 = FALSE; for (int i=len-1; i>=0; i--) { tempStr = (unsigned char)str[ i]; if (!gb2 && (tempStr>161) && (tempStr<254)) { gb2 = TRUE; } else if (gb2 && (tempStr> 161) && (tempStr<247)) { gbNum++; gb2 = FALSE; } else { gb2 = FALSE; } if ((tempStr&0xc0) == 0x80 ) { counter ++; head = head>>1; head |= 0x80; } else if (counter > 0) { if ((((head>>1)|0x80)&tempStr) == head) { utf8Num++; head = 0x80; counter = 0; } } else { head = 0x80; counter = 0; } } if (gbNum*2 <= utf8Num*3) { return TRUE; } else { return FALSE; }}
QString string;if (!isUtf8(ba.constData())){ QTextCodec *codec = QTextCodec::codecForLocale(); string = codec->toUnicode(ba); }else string = QString::fromUtf8(ba);