C++ (Qt)QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));QString s("привет");qDebug() << s;
int main(int argc, char *argv[]){ QApplication a(argc, argv); QTextCodec *textCodec = QTextCodec::codecForName("UTF-8"); QTextCodec::setCodecForCStrings(textCodec); QTextCodec::setCodecForLocale(textCodec); QTextCodec::setCodecForTr(textCodec); MainWindow w; w.show(); return a.exec();}
void MainWindow::on_loadText_clicked(){ QSettings settings("settings.ini", QSettings::IniFormat); settings.setIniCodec("UTF-8"); ui->testEdit->setText(settings.value("TestEdit").toString()); QByteArray s; QString text = settings.value("TestEdit").toString(); qDebug() << text; s.append(text.toLocal8Bit()); qDebug() << s; s = text.toAscii(); qDebug() << s; s = text.toLatin1(); qDebug() << s; s = text.toLocal8Bit(); qDebug() << s; s = text.toUtf8(); qDebug() << s;}void MainWindow::on_saveText_clicked(){ QSettings settings("settings.ini", QSettings::IniFormat); settings.setIniCodec("UTF-8"); settings.setValue("TestEdit", ui->testEdit->text());}
void customMessageHandler(QtMsgType type, const char *msg){ QTextCodec *consoleCodec = QTextCodec::codecForName("CP1251"); QTextStream error(stderr); error.setCodec(consoleCodec); error << QString::fromUtf8(msg) << endl << flush;}int main(int argc, char *argv[]){ QApplication a(argc, argv); qInstallMsgHandler(customMessageHandler); QTextCodec *textCodec = QTextCodec::codecForName("UTF-8"); QTextCodec::setCodecForCStrings(textCodec); QTextCodec::setCodecForLocale(textCodec); QTextCodec::setCodecForTr(textCodec); MainWindow w; w.show(); return a.exec();}