解决QTextBrowser控件调用setSource函数显示中文乱码的问题

本文主要是介绍解决QTextBrowser控件调用setSource函数显示中文乱码的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  自己发现的一个小技巧,有更好的方法欢迎大佬赐教。
  工程说明: 使用TextBrowser显示html可以进行html链接的导航,就是可以切换到上一链接界面或者下一个链接界面 但是我在使用的过程中发现,TextBrowser控件显示中文会出现乱码(textedit不会,不知道为什么),我是在windows平台上测试的,发现被加载的 html文件设置成utf16 LB编码格式就能正常显示,不过我已经完成了很多数量的html文件,一个一个的改或者找别的工作批量还是觉得不方便目前找到一种方法,在textBrowser控件执行setSource()函数后,再调用setHtml函数重新设置显示的文件这样不会出现中文乱码的情况。

  直接把工程源文件放上来了,直接拷贝用,另外测试的html文件就靠读者自备了。

//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QUrl>
#include <QDebug>
#include <QFile>
#include <QString>
#include <QTextCodec>//导航操作枚举值
enum NAVOPER{NAVOPER_NONE,NAVOPER_HOME,NAVOPER_PREV,NAVOPER_NEXT
};QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();void InitTextBrowserShowFileName(const QString & _qstr);private slots:void on_pushButton_last_clicked();void on_pushButton_next_clicked();void on_pushButton_home_clicked();void SlotUrlChanged(const QUrl &);void SlotAnchorClicked(const QUrl &);private:Ui::MainWindow *ui;void SetTextBrowserShowHtml(NAVOPER _oper);QString m_curfilefullpathfortextbrowser;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);InitTextBrowserShowFileName("./testhtml/1-html.html");SetTextBrowserShowHtml(NAVOPER_NONE);connect(ui->textBrowser,SIGNAL(sourceChanged(const QUrl &)),this,SLOT(SlotUrlChanged(const QUrl &)));connect(ui->textBrowser,SIGNAL(anchorClicked(const QUrl &)),this,SLOT(SlotAnchorClicked(const QUrl &)));
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::InitTextBrowserShowFileName(const QString &_qstr)
{m_curfilefullpathfortextbrowser = _qstr;
}void MainWindow::on_pushButton_last_clicked()
{SetTextBrowserShowHtml(NAVOPER_PREV);
}void MainWindow::on_pushButton_next_clicked()
{SetTextBrowserShowHtml(NAVOPER_NEXT);
}void MainWindow::on_pushButton_home_clicked()
{SetTextBrowserShowHtml(NAVOPER_HOME);
}void MainWindow::SlotUrlChanged(const QUrl & _url)
{m_curfilefullpathfortextbrowser = _url.toString();
}void MainWindow::SlotAnchorClicked(const QUrl & _url)
{m_curfilefullpathfortextbrowser = _url.toString();SetTextBrowserShowHtml(NAVOPER_NONE);
}void MainWindow::SetTextBrowserShowHtml(NAVOPER _oper)
{if(NAVOPER_HOME == _oper){ui->textBrowser->home();}else if(NAVOPER_PREV == _oper){ui->textBrowser->backward();}else if(NAVOPER_NEXT == _oper){ui->textBrowser->forward();}else if(NAVOPER_NONE == _oper){}else{return;}ui->textBrowser->setSource(m_curfilefullpathfortextbrowser,QTextDocument::HtmlResource);qInfo()<<"[show file] "<<m_curfilefullpathfortextbrowser;QFile file(m_curfilefullpathfortextbrowser);file.open(QIODevice::ReadOnly);QString temp = file.readAll();ui->textBrowser->setHtml(temp);file.close();
}
//mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="windowTitle"><string>MainWindow</string></property><widget class="QWidget" name="centralwidget"><layout class="QVBoxLayout" name="verticalLayout_2"><item><layout class="QVBoxLayout" name="verticalLayout"><item><widget class="QTextBrowser" name="textBrowser"><property name="html"><string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;meta charset=&quot;utf-8&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
hr { height: 1px; border-width: 0; }
li.unchecked::marker { content: &quot;\2610&quot;; }
li.checked::marker { content: &quot;\2612&quot;; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Microsoft YaHei UI'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string></property></widget></item><item><layout class="QHBoxLayout" name="horizontalLayout"><item><widget class="QPushButton" name="pushButton_last"><property name="text"><string>&lt;-</string></property></widget></item><item><widget class="QPushButton" name="pushButton_next"><property name="text"><string>-&gt;</string></property></widget></item><item><widget class="QPushButton" name="pushButton_home"><property name="text"><string>home</string></property></widget></item></layout></item></layout></item></layout></widget><widget class="QMenuBar" name="menubar"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>21</height></rect></property></widget><widget class="QStatusBar" name="statusbar"/></widget><resources/><connections/>
</ui>
//pro文件
QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.hFORMS += \mainwindow.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}

这篇关于解决QTextBrowser控件调用setSource函数显示中文乱码的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/314355

相关文章

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案

《电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案》最近有不少兄弟反映,电脑突然弹出“mfc100u.dll已加载,但找不到入口点”的错误提示,导致一些程序无法正... 在计算机使用过程中,我们经常会遇到一些错误提示,其中最常见的就是“找不到指定的模块”或“缺少某个DL

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Redis 热 key 和大 key 问题小结

《Redis热key和大key问题小结》:本文主要介绍Redis热key和大key问题小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、什么是 Redis 热 key?热 key(Hot Key)定义: 热 key 常见表现:热 key 的风险:二、

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制