Qt 之 qDebug()打印和QString中文乱码


前言

qt代码开发过程中,最常用的应该就是qDebug()了把,跟踪程序的执行先后流程,打印变量内容。


一、qDebug()?

使用qDebug()函数,它可以把调试信息直接输出到控制台上。

输出到控制台上有两种方式:

  (1) 将字符串当做参数传给qDebug()函数。(这种方法可以不用添加头文件#include<QDebug>)

  (2) 使用流输出的方法输出多个字符串。(需要添加 #include<QDebug>头文件)

二、使用方法

1.不用引入头文件

如果向函数传递格式字符串和参数列表,则其工作方式与C语言的printf()函数类似。格式应为Latin-1字符串

qDebug(const char *message, …)

qDebug(const char *message, ...)
qDebug("%s", "Hello world!");

2.引入头文件 #include

代码如下(示例):

#include <QDebug>

qDebug() << "Hello" << "world!";
qDebug() << QString("Hello world!");


int x = 100;
qDebug("x:%d",x);
//使用流的方法输出
int y = 250;
qDebug() << "--cout--" << endl << "x:" << x;
qDebug() << "y:" << y;

3.关闭自动插入空格

QDebug &QDebug::nospace()

qDebug() << "Hello" << "world!";
qDebug().nospace() << "Hello" << "world!";
输出:
Hello world!
Helloworld!

4.关闭引号字符,转义字符

禁用在 QChar,QString 和 QByteArray内容周围自动插入引号字符。当开启引号字符禁用时,这些类型的打印将不带引号字符,也不会转义不可打印的字符。

QDebug &QDebug::noquote()
qDebug() << QString("Hello world!");  
qDebug().noquote() << QString("Hello world!");
输出:
"Hello world!"
Hello world!

QDebug &QDebug::nospace()

qDebug() << "Hello" << "world!";
qDebug().nospace() << "Hello" << "world!";
输出:
Hello world!
Helloworld!

三、扩展

1.屏蔽qDebug()打印

首先,我们来看一段代码

DEFINES+= QT_NO_DEBUG_OUTPUT

2.qDebug和QString中的转义字符

项目文件(.pro)添加

#include <QCoreApplication>
#include <QDir>
#include <QDebug>
#include <iostream>
 
int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
 
    QString dir = app.applicationDirPath();
    dir = QDir::toNativeSeparators(dir);
 
    qDebug() << "qDebug:" << dir;
    std::cout << "cout:   " << dir.toStdString() << std::endl;
    printf("printf: ");
    for (auto ch : dir) {
        printf("%c", ch.toLatin1());
    }
 
    return app.exec();
}

输出如下:不感到意外把?
在这里插入图片描述
仔细观察,cout 和 printf 是一样的,唯独 qDebug() 输出了一些额外的东西;
qDebug() 不仅会在引号中打印字符串、转换 Unicode,还会完整打印转义字符,例如:\ " \t \n 等等
所以,如果想避免可以使用上面提到的***QDebug::noquote()***.

#include <QCoreApplication>
#include <QDir>
#include <QDebug>
#include <iostream>
 
int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
 
    QString dir = app.applicationDirPath();
    dir = QDir::toNativeSeparators(dir);
 
    qDebug().noquote() << "qDebug:" << dir;
    std::cout << "cout:   " << dir.toStdString() << std::endl;
    printf("printf: ");
    for (auto ch : dir) {
        printf("%c", ch.toLatin1());
    }
 
    return app.exec();
}

在这里插入图片描述

2.qDebug和QString中的中文乱码

下面代码环境,windows下,main.cpp为utf-8+bom,两种编译器msvc+ mingw下执行的不同结果

1. msvc情况一:

#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QTextCodec>
//#pragma execution_character_set("utf-8")  //注释掉这句

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    QString n2 = QString::fromUtf8("你好马");
    QString n("你好");
    qDebug()<<"中文"<<n.toStdString().c_str() << n << n2;

    MainWindow w;
    w.show();
    return a.exec();
}

输出如下:

???? ???? "????" "??????"

结果,中文为乱码。

2. msvc情况二:

#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QTextCodec>
#pragma execution_character_set("utf-8")  //放开这句话

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    QString n2 = QString::fromUtf8("你好马");
    QString n("你好");
    qDebug()<<"中文"<<n.toStdString().c_str() << n << n2;

    MainWindow w;
    w.show();
    return a.exec();
}

输出如下:

中文 你好 "你好" "你好马"

结果正确。

2. mingw情况下:

结果均正确

乱码总结

QT中还有2个概念非常重要:

源码字符集(the source character set)源码文件是使用何种编码保存的
执行字符集(the execution character set)可执行程序内保存的是何种编码(程序执行时内存中字符串编码)

总体来说,解决方法就一个:你必须知道源码的编码和执行的编码。

执行的编码我们也可以这样修改:

#pragma execution_character_set("utf-8")

QT的QString默认情况下只能正确显示UTF-8编码的字符串,因此,我们必须保证,要显示的字符串首先要转换为UTF-8,再进行显示。当然,我们也可以用代码人为的设定QString的编码。

然而,默认情况下,QT的编辑器是GB2312编码,QString是UTF-8编码,这时就会乱码,可以使用下文的方法一,把编码转为QString支持的编码

最简洁的方法就是:把源码改为UTF-8编码,这样不用做任何转换,直接就能正确显示,例如:

QString str="这是汉字";
ui->textEdit->append(str);

如果源码为GB2312,QString默认为为UTF-8,这时,要这样写才不会乱码:

QString str= QString::fromLocal8Bit("这是汉字");
ui->textEdit->append(str);

这个例子是把本地编码(我们的操作系统得编码大多为GB2312),转换为QString支持的编码。

总结

qDebug() 输出中文乱码,或者qt的编码问题,常常困扰这我们啊。

扩展阅读utf-8 BOM

BOM:byte order mark,定义字节顺序,因为网络传输中分为两种,大头和小头。uft-8不需要bom表明字节顺序,但可以用BOM来表示编码方式,windows就是采用bom来标记文本文件的编码方式的。

bom是为utf-16和utf-32准备的,用于标记字节顺序。微软在utf-8中使用bom是因为这样可以把UTF-8和ASCII等编码区分开来,但这样的文件在windows之外的操作系统里会带来问题。

  • 19
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Qt中,如果在QDebug中文显示乱码,可能有几种情况和解决方法。根据引用\[1\]和引用\[2\]的内容,可以得出以下解决方法: 1. 如果原代码是gbk格式编码,可以使用QTextCodec来解决乱码问题。在代码中添加以下代码: ```cpp QTextCodec *corder = QTextCodec::codecForName("GBK"); qDebug() << corder->toUnicode("中文内容"); ``` 这样可以将中文内容正确显示出来。 2. 如果使用的是msvc编译器,可以在main函数中添加以下代码: ```cpp QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); ``` 这样可以将中文内容以UTF-8编码正确显示出来。 综上所述,根据不同的情况,可以采取相应的解决方法来解决Qt中QDebug中文显示乱码的问题。 #### 引用[.reference_title] - *1* *3* [QT乱码问题(包含linux和windos下控件、文本、qDebug输出乱码)](https://blog.csdn.net/qq_37131073/article/details/123688529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Qt 之 qDebug()打印QString中文乱码](https://blog.csdn.net/u011942101/article/details/116095268)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值