解决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

相关文章

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分