Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Qt 應用移植至開發板後無法顯示中文解決方案

編輯:關於 Linux

Qt 在 Ubuntu 桌面環境可正常顯示中文,但交叉編譯至嵌入式開發板時常出現方塊字或亂碼。核心原因有二:字元編碼設定與目標板缺少中文字型。

解決步驟

  1. 統一採用 UTF-8 編碼,在專案 .pro 檔與原始碼中確保原始檔編碼為 UTF-8。
  2. 複製字型至目標板:從 Ubuntu 宿主機 /usr/share/fonts/truetype/wqy/ 複製 wqy-microhei.ttc 等字型至開發板 Qt 字型目錄(如 /opt/qt-4.8.4/lib/fonts/)。
  3. 設定環境變數 指定 Qt 字型搜尋路徑:
    export QT_QWS_FONTDIR=/opt/qt-4.8.4/lib/fonts
  4. 在 main.cpp 強制設定編碼與預設字型:
    #include <QApplication>
    #include <QTextCodec>
    #include <QFont>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QTextCodec *codec = QTextCodec::codecForName("utf8");
        QTextCodec::setCodecForLocale(codec);
        QTextCodec::setCodecForCStrings(codec);
        QTextCodec::setCodecForTr(codec);
    
        QFont font;
        font.setFamily("wqy-microhei");
        a.setFont(font);
    
        // 介面字串使用 tr() 宏
        // ui->label->setText(tr("攝像頭已經運行!"));
    
        return a.exec();
    }

開發板中文顯示

完成上述步驟後,開發板上即可正常渲染中文,字型大小、換行排版皆無異常。

Copyright © Windows教程網 All Rights Reserved