解决Qt程序发布时中文乱码问题(通过QApplication.addLibraryPath加载QTextCodec插件)
参考:
https://www.cnblogs.com/findumars/p/5624851.html
Qt程序的文字编码,是通过插件来解决的,所以我们发布的时候需要把相应的插件也发布出去,在开发者电脑上程序会自动从插件目录加载到插件,但是如果发布给别的电脑使用,需要手动指定插件路径,如下所示:
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString strLibPath = a.applicationDirPath();
    strLibPath += "/plugins";  //plugins就是插件目录
    a.addLibraryPath(strLibPath);  //此代码必须在QTextCodec的代码前执行
    QTextCodec *pcodec = QTextCodec::codecForName("gb2312");
    QTextCodec::setCodecForCStrings(pcodec);
    QTextCodec::setCodecForLocale(pcodec);
    QTextCodec::setCodecForTr(pcodec);
    CWinMain w;
    w.show();
    return a.exec();
}
备注:这个plugins目录就是Qt SDK(注意不是QtCreator的)的plugins目录,对于文字编码的子目录是codecs,如果用到其它插件也相应复制其它插件目录发布.
posted on 2019-06-04 16:01 crystal_rachel 阅读(1157) 评论(0) 收藏 举报
                    
                
                
            
        
浙公网安备 33010602011771号