qt for vs 界面显示乱码 及 Qt Creator中显示中文乱码问题

转自 https://blog.csdn.net/u012230798/article/details/87947227

1:在CPP文件里加入 以下代码  , 在#include 后面加入,否则  #ifdef Q_OS_WIN   不被识别。  也可以直接不要Q_OS_WIN。解决乱码的是这一句:   

#pragma execution_character_set("utf-8")
#ifdef Q_OS_WIN
#pragma execution_character_set("utf-8") //解决 VS编译器下中文乱码
#endif
 

我的

#include "plc_dialog.h"
#include "ui_plc_dialog.h"
#include "ioselect_dialog.h"
 
#ifdef Q_OS_WIN
    #pragma execution_character_set("utf-8")   //解决 VS编译器下中文乱码
#endif
 
PLC_Dialog::PLC_Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PLC_Dialog)
{
    ui->setupUi(this);
 
    isEnt = false;
 
   type = "";
   port = 0;
   Level = 0;
 
}

  

 

2:如果不行,右键查看文件的格式   选择 UTF-8 BOM on Save  要将文件保存为 UTF-8 BOM模式,  保存的时候一定要写的东西,刺激编辑器保存文件,否则还是改不了。 

 

 

 

 

就是这种类型的错误:

 

 

 

 

保存为 UTF-8 BOM模式时注意事项:  Ctrl + S  保存。

 

 

 

 

 

其他转换问题参考 https://blog.csdn.net/technologyleader/article/details/81979450?utm_source=distribute.pc_relevant.none-task

Qt中的中文显示,经常会出现乱码。从网上看了一些博客,大都是Qt4中的解决方法, 
网上搜到的都是这种:

#include < QTextCodec >
int main(int argc, char **argv)
{
....................
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF8"));
..........................
}

  

Qt5中, 取消了QTextCodec::setCodecForTr()和QTextCodec::setCodecForCString()这两个函数,而且网上很多都是不推荐这种写法。

我的问题

代码:

#include "helloqt.h"
#include <QtWidgets/QApplication>
#include <qlabel.h>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    HelloQt w;
    w.setWindowTitle("学生事务管理系统");
    w.resize(300, 140);
 
    QLabel label("test",&w);
    label.setGeometry(100, 50, 160, 30);
    w.show();
    return a.exec();
}

 

 

 

 

结果: 
这里写图片描述

解决方法

有三种转换的方法: 
1.加上#include <qtextcodec.h> 
QTextCodec *codec = QTextCodec::codecForName(“GBK”);//修改这两行 
w.setWindowTitle(codec->toUnicode(“学生事务管理系统”)); 
代码改为:

#include "helloqt.h"
#include <QtWidgets/QApplication>
#include <qlabel.h>
#include <qtextcodec.h>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    HelloQt w;
    QTextCodec *codec = QTextCodec::codecForName("GBK");//修改这两行
    w.setWindowTitle(codec->toUnicode("学生事务管理系统"));
    w.resize(300, 140);
 
    QLabel label("test",&w);
    label.setGeometry(100, 50, 160, 30);
    w.show();
    return a.exec();
}

 

 

 

 

2.w.setWindowTitle(QString::fromLocal8Bit(“学生事务管理系统”)); 
代码改为:

#include "helloqt.h"
#include <QtWidgets/QApplication>
#include <qlabel.h>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    HelloQt w;
    w.setWindowTitle(QString::fromLocal8Bit("学生事务管理系统"));//修改这一行
    w.resize(300, 140);
 
    QLabel label("test",&w);
    label.setGeometry(100, 50, 160, 30);
    w.show();
    return a.exec();
}

 

 

 

 

3.w.setWindowTitle(QStringLiteral(“学生事务管理系统”)); 
代码改为:

#include "helloqt.h"
#include <QtWidgets/QApplication>
#include <qlabel.h>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    HelloQt w;
    w.setWindowTitle(QStringLiteral("学生事务管理系统"));//修改这一行
    w.resize(300, 140);
 
    QLabel label("test",&w);
    label.setGeometry(100, 50, 160, 30);
    w.show();
    return a.exec();
}

 

 

 

 

结果: 
这里写图片描述

 

posted @ 2020-02-21 15:54  阳光下的小土豆  阅读(1140)  评论(0编辑  收藏  举报