QT5 文字化け UTF8のソースを読んでも、文字化けする
漢字のところが、文字化けする。
UTF8でソースを作ってるのに、うまくいかない。
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFontDatabase> #include <QMessageBox> #include <QTextCodec> #include <QString> #include <QFile> #define TR(s) (QString::fromLocal8Bit(s)) bool MainWindow::canOpenFontFile(QString fileName) { if (fileName.isEmpty()) { return false; } else { QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { QMessageBox::information( this, TR("Unable to open file:\n"), file.errorString()+ "\n"+fileName); return false; } } return true; } bool MainWindow::canAddDb(QFontDatabase database, QString fileName) { if (canOpenFontFile(fileName)) { int result = database.addApplicationFont(fileName); if (result == -1) { QMessageBox::information( this, TR("Unable to install font file:\n"), "fileName="+fileName); return false; } } return true; } MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // QString path = QCoreApplication::applicationDirPath() + "/"; // QString fileName = path +"org.ttf"; QString path = ":/fonts/"; QString fileName = path + "org.ttf"; QFontDatabase database; if(canAddDb(database, fileName)) { QFont f = database.font("myfont","Normal",30); f.setFixedPitch(true); f.setPixelSize(12); ui->label01->setFont(f); ui->label02->setFont(f); ui->label03->setFont(f); } QString str01 = path; QString str02 = TR("!#$%& 0123456789 abcdefghijklmnopqrstuvwxyz 美しい文字 あいうえお"); QString str03 = TR("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); ui->label01->setText(str01); ui->label02->setText(str02); ui->label03->setText(str03); QString str11 = path; ui->label11->setText(str11); ui->label12->setText(TR("!#$%& 0123456789 abcdefghijklmnopqrstuvwxyz 美しい文字 あいうえお")); ui->label13->setText(TR("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); } MainWindow::~MainWindow() { delete ui; }